405 - Method Not Allowed HttpWebRequest

前端 未结 3 829
萌比男神i
萌比男神i 2021-01-25 03:54

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          


        
相关标签:
3条回答
  • 2021-01-25 04:29

    There is nothing wrong with the POST request, so the problem must lie elsewhere.

    There are a number of other places along the way that could be causing trouble:

    1. There could be a bad proxy between you and the server that is changing the HTTP method
    2. The server could be malfunctioning or simply no support POST

    My guess, however, is that the server is doing more than just looking at the HTTP method and is instead giving you a 405 error in response to the RPC call that your XML payload is wrapping.

    Some RPC servers will (erroneously) use HTTP status codes like this to indicate that the requested method cannot be executed, or that something else is wrong with the request that is of a permissions and security nature.

    If the server is slightly better behaved (and you are lucky), it should be returning additional information in the response body that might indicate where the 405 error is coming from.

    0 讨论(0)
  • 2021-01-25 04:48

    Your POST appears to be completely invalid. POST data is supposed to be encoded (ie, as multipart/form-data) and use correct content-type (ie, application/x-www-form-urlencoded) with proper encoding and boundaries etc. You are just sending the server a lump of text and I'm not surprised it flips out.

    I'm not 100% sure what VB is doing behind the scenes but this MSDN page suggests you need to set the content-type to a supported method and you probably need Content-Disposition: form-data in your headers as well. I found an example that does this and adds the MIME boundaries:

    string FileData = "this is test file data\r\n"; // test data to send.
    StringBuilder DataString = new StringBuilder();
    DataString.Append(dataBoundary + "\r\n");
    //This sends the viewstate info
    DataString.Append("Content-Disposition: form-data; name=" + HiddenValue
    + "\r\n"
    + dataBoundary + "\r\n");
    DataString.Append("Content-Disposition: form-data; name=" + "\"" +
    "File1" +
    "\"" +
    "; filename=" + "\"" + "TestFile3.txt" + "\"" + "\r\n");
    DataString.Append("Content-Type: text/plain\r\n\r\n");
    DataString.Append(FileData);
    DataString.Append(dataBoundary + "\r\n");
    DataString.Append("Content-Disposition: form-data; name=" + "\"" +
    "Submit1" +
    "\"" + "\r\n\r\n" + "Upload\r\n" + dataBoundary + "--\r\n");
    

    That example emulates a file field upload.

    For a simpler version you can use request.ContentType = "application/x-www-form-urlencoded" with URL-encoded data like: Dim postData As String = "myURL=http%3A%2F%2Fexample.com%2Findex.php". Note that the format is key=value and the data must be URL-encoded first.

    I'm not sure what exactly you need though because a lot depends on what the remote server actually expects for the form and field names. It also depends wether the server is actually following the relevant HTML standards and RFCs. Do you have a working HTML form to use as a guide for what the server expects?

    0 讨论(0)
  • 2021-01-25 04:51

    Are you missing the Content-Type header required for a POST ? Read here for details. I assume it is mandatory.

    0 讨论(0)
提交回复
热议问题