Locked 405 - Method Not Allowed HttpWebRequest POST

  • 2012年4月4日 10:02
     
      コードあり

    I have a problem when trying to send a POST request. The sending method looks like this:

    Public Sub SendXML(ByVal file As String) 
        Dim reader As New StreamReader(file) 
        Dim data As String = reader.ReadToEnd() 
        reader.Close() 
        Dim request As HttpWebRequest = WebRequest.Create("http://blah/Request") 
        request.Method = "POST" 
     
        System.Net.ServicePointManager.Expect100Continue = False 
     
        Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(data) 
        request.ContentLength = bytes.Length 
     
        Dim oStreamOut As Stream = request.GetRequestStream() 
        oStreamOut.Write(bytes, 0, bytes.Length) 
        oStreamOut.Close() 
     
        Dim response As HttpWebResponse = request.GetResponse() 
     
    End Sub 
    

    When running this I get the above error. Through Fiddler I can see that the request looks like:

    POST http://blah/Request HTTP/1.1 
    Host: blah 
    Content-Length: 322 
    Proxy-Connection: Keep-Alive 
     
    <?xml version="1.0"?> 
    <Envelope> 
    <Header> 
    <UserID>uid</UserID> 
    <Password>pass</Password> 
    <SessionID /> 
    <RequestType>GetDetails</RequestType> 
    <POSCompany>01</POSCompany> 
    <PackageType>DATA</PackageType> 
    <ActionType>READ</ActionType> 
    <SnoopUserID /> 
    </Header> 
    <Body> 
    <MagicNumber>124</MagicNumber> 
    </Body> 
    </Envelope> 
    

    Now looking at this I suspected that it was due to the fact that the server does not accept POST messages. But some other reading suggests that the the URI http://blah/Request has been generated with a proxy and should be /Request so the line should read POST /Request HTTP/1.1

    So what would be the common reason for this? And if it is a proxy problem, how is it sorted?

すべての返信

  • 2012年4月4日 14:01
     
     

    The "http://blah/Request" part of the request appears in request when you configure the client to go via HTTP proxy. In this case the proxy will change the request when it's sent to the final destination. What you see is what goes to the proxy, not to the server.

    As there's a proxy involved, it's very likely that the proxy changes the request in some way that the server stops accepting it for whatever reason.

    So the first thing to do is switch from using proxy to direct connection. Once you have direct connection working, you can make scheme more complicated by bringing in the proxy.


    Sincerely yours, Eugene Mayevski

  • 2012年4月4日 14:10
     
     
    My question though would be, how do you configure to use a direct connection? I have tried everything that I can find to change this but keep getting the same message. What should I be doing to use a direct connection?
  • 2012年4月4日 15:55
     
     
    Also, how am I able to intercept the actual message on the wire to check what has been sent rather than what would arrive at the proxy?
  • 2012年4月5日 4:23
     
     

    I have some thought...

    Are you sure it's really accepting POST, I thought web service expect SOAP verb by default. Better check the documentation or verify with vendor for this.

    If the web service is a new one created by your company and you're sure it should accept POST, check web.config setting to see if your handler allows "verb" "POST". 

  • 2012年4月5日 10:07
     
     

    It is a third party server, but they have advised me that POST methods do work. My first suspicion was that it wasn't accepting the POST method but they say they have reaffirmed that it in fact does.

    I am still unsure about this though, as they are unable to provide any example .Net code that works. They have provided, what they define as, a working XML which is exactly as above other than the initial line looking like POST /Request HTTP/1.1

  • 2012年4月5日 10:49
     
     

    Perheps if their web service expose WSDL contract, you can try to directly "Add Web Reference" to the project (or use wsdl.exe) so you get a proxy class that you can be sure must work.

    Many of them does offer this development aid. For example, Entrust Identity Gateway offer this as a setting disabled by default (expose WSDL to public can have security implication and is generally avoided). Perheps you can ask your vendor about that. 

  • 2012年4月8日 4:59
     
     

    I am interested in what is the response you received in Fiddler that contains the 405 error.

    The response itself (headers and contents) contains much useful information for troubleshooting but you don't even remember to paste it here.


    Lex Li (http://lextm.com)