HTTP请求重启Internet连接

本文关键字:连接 Internet 重启 请求 HTTP | 更新日期: 2023-09-27 18:15:19

最近我开始为我的windows phone 8应用程序开发web API,当我尝试将其连接到我的应用程序(通过WebClient, HttpRequest和HttpClient)时,它以同样的方式结束,只是断开我的互联网连接而没有任何信息。我要访问的站点是gtlol.necroworks.net/free。还有其他人遇到类似的情况和/或找到了答案吗?

更多描述:

HttpClient和WebClient请求示例:

//HttpClient example
using (HttpClient hc = new HttpClient())
{
   hc.BaseAddress = new Uri("http://gtlol.necroworks.net/");
   HttpResponseMessage response = await hc.GetAsync("free");
   if (response.IsSuccessStatusCode)
   {
       var res = await response.Content.ReadAsStringAsync();
       MessageBox.Show(res);
   }
   else
   {
       MessageBox.Show("Not working - no internet connection");
   }
}
//WebClient example
WebClient freeRotationWebClient = new WebClient();
freeRotationWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_GetRotation);
freeRotationWebClient.DownloadStringAsync(new Uri("http://gtlol.necroworks.net/free"));
private void webClient_GetRotation(object sender, DownloadStringCompletedEventArgs e)
{
    try
    {
        if (!e.Cancelled)
        {
            MessageBox.Show((string)e.Result);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Not working");
    }
}

em都应该返回这个结果:

[{"_id":"53d264aa6e65634e49000000","champId":53},{"_id":"53d264ab6e65634e49010000","champId":36},{"_id":"53d264ab6e65634e49020000","champId":79},{"_id":"53d264ab6e65634e49030000","champId":96},{"_id":"53d264ab6e65634e49040000","champId":75},{"_id":"53d264ab6e65634e49050000","champId":72},{"_id":"53d264ac6e65634e49060000","champId":48},{"_id":"53d264ac6e65634e49070000","champId":6},{"_id":"53d264ac6e65634e49080000","champId":101},{"_id":"53d264ac6e65634e49090000","champId":115}]

但是当任何请求试图获得这个结果时,它只是在互联网连接重置中结束,没有任何消息或任何警告或任何异常。我唯一能从调试器中得到的是请求得到状态码404的信息。

我使用的任何其他web API服务都可以正常工作。

我注意到的一件事是头我从我的api是:

Accept-Ranges →bytes
Age →0
Connection →keep-alive
Content-Length →482
Content-Type →application/json;charset=utf-8
Date →Wed, 30 Jul 2014 18:44:45 GMT
Server →nginx
Via →1.1 varnish
X-Content-Type-Options →nosniff
X-Varnish →584616320

我要缓存的api使用:

Access-Control-Allow-Headers →Content-Type
Access-Control-Allow-Methods →GET, POST, DELETE, PUT
Access-Control-Allow-Origin →*
Connection →keep-alive
Content-Encoding →gzip
Content-Length →161
Content-Type →application/json; charset=UTF-8
Date →Wed, 30 Jul 2014 18:45:13 GMT
Server →Jetty(9.1.1.v20140108)
Vary →Accept-Encoding, User-Agent
X-NewRelic-App-Data →#string of chars here

HTTP请求重启Internet连接

您正在从公共HTTP缓存获得响应,而不是从原始服务器获得响应。缓存(Varnish)可能已经存储了先前404响应的副本,现在很高兴地为您的请求提供响应,并将一直持续到响应失效为止。多长时间取决于服务器设置的max-age/expires。

你可以尝试在你的请求中添加一个cache-control头,表明你不想得到一个缓存的响应。试着做,

var request = new HttpRequestMessage() {
                             RequestUri = new Uri("free",UriKind.RelativeOrAbsolute)
                          }; 
request.Headers.CacheControl = new CacheControlHeader() {NoCache=true};
HttpResponseMessage response = await hc.SendAsync(request);

请注意,我在写这篇文章时没有智能感知,也没有编译器,所以请注意拼写错误!