405 - 方法不允许HttpWebRequest [英] 405 - Method Not Allowed HttpWebRequest

查看:465
本文介绍了405 - 方法不允许HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试发送 POST 请求时遇到问题。发送方法如下所示:

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

运行此操作时出现上述错误。通过Fiddler我可以看到请求看起来像:

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>

现在看着这个我怀疑这是因为服务器不接受 POST 消息。但是其他一些读物表明URI http:// blah / Request 已经使用代理生成,应该是 / Request 所以该行应该是 POST / Request HTTP / 1.1

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?

如下所述,我为更具体的请求创建了一个新问题。 在HTTP POST标头中将绝对URI更改为相对

As asked below, I have created a new question for the more specific request. Changing absolute URI to relative in HTTP POST header

推荐答案

POST请求没有任何问题,所以问题必须在其他地方。

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. 之间可能存在错误的代理您和正在更改HTTP方法的服务器

  2. 服务器可能出现故障或根本没有支持POST

然而,我的猜测是,服务器不仅仅是查看HTTP方法,而是为了响应XML负载正在包装的RPC调用而给出405错误。

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.

某些RPC服务器将(错误地)使用这样的HTTP状态代码来指示无法执行所请求的方法,或者请求的其他内容是错误的这是具有权限和安全性的。

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.

如果服务器略微表现得更好(而且你很幸运),那么应该在响应正文中返回可能指示405错误来自何处的附加信息。

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.

这篇关于405 - 方法不允许HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆