Silverlight HttpWebRequest的内容长度为空,是WTF吗? [英] Silverlight HttpWebRequest content length empty, WTF?

查看:68
本文介绍了Silverlight HttpWebRequest的内容长度为空,是WTF吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,接收请求的RESTful Web服务的主体为空(内容长度= 0),我不知道为什么?

When I run the following code the RESTful web service receiving the request has an empty body (content length = 0) and I don't know why?

如果我在调试时运行Fiddler,请求将按预期执行,并且服务器接收到主体,我想我没有为请求配置任何想法吗?

If I run Fiddler whilst debugging the request executes as expected and the server receives the body, I guess I'm not configuing something for the request any ideas?

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.ContentType = "text/xml";
        request.Method = "POST";
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(uri, new Cookie("SSOToken", HttpUtility.UrlEncode(SsoToken)));
        request.ContentLength = data.Length;

        request.BeginGetRequestStream(ar1 =>
        {
            var byteArray = Encoding.UTF8.GetBytes(data);

            var stream = request.EndGetRequestStream(ar1);
            stream.Write(byteArray, 0, data.Length);
            stream.Close();

            request.BeginGetResponse(ar2 => HandleSearchCompleted(ar2, request, action), state);
        }, state);

推荐答案

它是哪种服务器?如果客户端使用的是HTTP/1.1协议(默认情况下使用的是HTTP/1.1协议),则它将不会随请求一起发布整个实体.相反,它首先只是发送带有Expect:100-continue标头的标头...

What kind of server is it? If the client is using HTTP/1.1 protocol (which is what it uses by default) then it wont post the entire entity body with the request. Instead it first just sends the headers, with an Expect: 100-continue header...

POST/url HTTP/1.1

POST /url HTTP/1.1

主机:主机名

内容长度:128

期望:100-继续

此时,如果服务器已准备好接受数据,则应使用以下命令进行回复:

At this point, if the server is ready to accept the data, it should reply with:

HTTP/1.1 100继续

HTTP/1.1 100 Continue

因此,可能是您正在发布到无法正确理解请求的越野车服务器.这就是为什么当您通过Fiddler进行发布时,服务器最终会获得实体,因为Fiddler可能正在将请求标头和实体正文发送到服务器(在与客户端进行HTTP/1.1 100继续握手之后).

So, it could be that you are posting to a buggy server that is not understanding the request correctly. That is why, when you post through Fiddler, the server ends up getting the entity because fiddler is probably sending the request headers and entity body to the server (after doing the HTTP/1.1 100 continue handshake with the client).

解决方法?

  1. 尝试在HttpWebRequest上设置Expect100Continue = false.
  2. 尝试使用HTTP/1.0协议.

这篇关于Silverlight HttpWebRequest的内容长度为空,是WTF吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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