WebClient不处理KeepAlive - 将不需要的文本添加到JSON正文

时间:2015-03-31 16:14:19

标签: .net json webclient

我有一个非常奇怪的问题,我似乎无法追查。我将小型JSON对象发布到Web服务并获取表示为字符串的大型JSON对象。我遇到的问题是随机返回的JSON对象中我得到以下文本:

  

DNT:1连接:关闭

当我尝试反序列化JSON时,这显然会引发。起初它是消息正文中的Expect100-Continue,然后它是KeepAlive的东西,现在它是Connection:Close东西。我只是使用标准的WebClient,但是我创建了一个继承自WebClient的新类,所以我可以正确设置标题,这里是:

public class CustomWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).KeepAlive = false;
            (request as HttpWebRequest).ServicePoint.Expect100Continue = false;
        }
        return request;
    }
}

这是我的电话实施:

using (var webClient = new CustomWebClient())
{
    webClient.Proxy = null;
    webClient.Headers[HttpRequestHeader.ContentType] = Constants.HttpJSONencodedHeader;

    return webClient.UploadData(request.Url, Constants.HttpPostVerb, Encoding.ASCII.GetBytes(request.Params.GetFormattedJSon()));
}

我期待的是一个JSON对象,如下所示:

(简化的)

"276425": {
   "min": 1.0,
   "max": 1.0,
   "count": 1,
   "missing": 0,
   "distinctValues": [1],
   "countDistinct": 1,
   "sum": 1.0,
   "sumOfSquares": 1.0,
   "mean": 1.0,
   "stddev": 0.0,
   "facets": {}
}

这是偶尔随机回来的:

"280950": {
   "min": 1.0,
   "max": 1.0,
   "count": 1,
   "missing": 0,
   "distinctValues": [1],
   "countDistinct": 1,
   "sum": 1.
   DNT: 1
   Connection: close "stddev": 0.0,
   "facets": {}
}

要突出显示的部分是我在json对象的中间有这个权利:

 .
        DNT: 1
        Connection: close

非常感谢任何和所有帮助/建议。

编辑:响应是~8 MB的文本,所以我不想发布完整的回复,但这里是标题:

HTTP/1.0 200 OK
Set-Cookie: ServerID=1139; path=/
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ServiceStack/3.971 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Tue, 31 Mar 2015 15:42:41 GMT
Connection: close
Content-Length: 5802075

编辑#2: 网络布局:

Client - ASP.NET 4.0 (IIS 7, Server 2012)
  Web Service - ASP.NET 4.0 ServiceStack v3.9.71.0
     SOLR 5 - Jetty (Linux, iunno some flavour)

0 个答案:

没有答案