我可以从 C# 向 IIS 发送空的 HTTP POST WebRequest 对象吗?

发布于 2024-11-05 17:10:44 字数 949 浏览 4 评论 0原文

我是否需要在 WebRequest 对象中放入一些随机垃圾数据来绕过 IIS 上的 HTTP 状态代码 411 限制?

我在 MVC 3 应用程序中有一个 HttpPost 操作方法,它使用 POST 请求以及在查询字符串中传递的所有相关信息(不需要正文)。

[HttpPost] public ActionResult SignUp(string email) { ... }

它在 Visual Studio 的内置 Web 主机 Cassini 中运行良好。不幸的是,一旦 MVC 代码在 IIS [2008 R2 上的 7.5] 上运行,当我从外部 C# 表单应用程序中点击它时,服务器会返回一个 HTTP 错误代码。

远程服务器返回错误: (411) 需要长度。

这是调用代码:

WebRequest request = WebRequest.Create("http://somewhere.com/signup/[email protected]");
request.Method = "POST";
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader responseReader = new StreamReader(responseStream)) {
    // Do something with responseReader.ReadToEnd();
}

Do I need to just slap some random garbage data in a WebRequest object to get by the HTTP status code 411 restriction on IIS?

I have an HttpPost action method in an MVC 3 app that consumes a POST request with all the relevant information passed in the querystring (no body needed).

[HttpPost] public ActionResult SignUp(string email) { ... }

It worked great from Visual Studio's built in web host, Cassini. Unfortunately, once the MVC code was live on IIS [7.5 on 2008 R2], the server is pitching back an HTTP error code when I hit it from my outside C# form app.

The remote server returned an error:
(411) Length Required.

Here is the calling code:

WebRequest request = WebRequest.Create("http://somewhere.com/signup/[email protected]");
request.Method = "POST";
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader responseReader = new StreamReader(responseStream)) {
    // Do something with responseReader.ReadToEnd();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-11-12 17:10:44

事实证明,您只需在发送请求之前在请求上添加一个空的内容长度即可完成此操作。

WebRequest request = WebRequest.Create("http://somewhere.com/signup/[email protected]");
request.Method = "POST";
request.ContentLength = 0;

不确定明确给出空长度与暗示空长度有何不同,但 IIS 在我这样做后很高兴。可能还有其他方法可以解决这个问题,但这看起来很简单。

Turns out you can get this to go through by simply slapping an empty content length on the request before you send it.

WebRequest request = WebRequest.Create("http://somewhere.com/signup/[email protected]");
request.Method = "POST";
request.ContentLength = 0;

Not sure how explicitly giving an empty length vs. implying one makes a difference, but IIS was happy after I did. There are probably other ways around this, but this seems simple enough.

对岸观火 2024-11-12 17:10:44

我相信您每次向 Web 服务器发布请求时都需要设置 Content-Length 标头:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.contentlength.aspx

您可以尝试 GET 请求来测试它。

I believe you are required to set a Content-Length header anytime you post a request to a web server:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.contentlength.aspx

You could try a GET request to test it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文