2015-06-03 32 views
0

这段代码被写入以向服务发送请求并接收简单的json响应,但服务的服务器日志显示它正在被请求两次,延迟1秒。我无法弄清楚为什么这段代码提出请求两次,请指导我如果有一些配置丢失。它为什么要发送两次请求

string StrUrl = @"http://www.myapp.com/Tracker.json"; 
Uri uri1 = new Uri(StrUrl); 
HttpWebRequest webRequest1 = (HttpWebRequest)HttpWebRequest.Create(uri1); 
webRequest1.ServicePoint.ConnectionLimit = 1; 
webRequest1.Timeout = 50000; 
webRequest1.ReadWriteTimeout = 50000 
webRequest1.PreAuthenticate = false; 
webRequest1.Proxy = null; 
webRequest1.CookieContainer = cookieJar; 
webRequest1.Method = "POST"; 
webRequest1.KeepAlive = false; 

WebResponse response1 = webRequest1.GetResponse(); 

StreamReader streamReader1 = new StreamReader(response1.GetResponseStream()); 
String responseData1 = streamReader1.ReadToEnd(); 

这些可能也有类似的问题

Why my Http client making 2 requests when I specify credentials?

https://social.msdn.microsoft.com/Forums/vstudio/en-US/dad87752-42dd-4346-a1c5-da30f6156406/why-httpwebrequest-send-data-twice-in-https

+2

看起来不错。也许你打电话给这个功能两次? –

+0

检查你的代码,看看你是否调用两次函数。 – user1666620

+0

我已经在调试器中测试了带有断点的代码。它只运行一次 – Saifee

回答

0

这个问题通过使用RestSharp (Simple REST and HTTP API Client for .NET)解决。

var client = new RestClient("http://www.myapp.com/Tracker.json"); 
// client.Authenticator = new HttpBasicAuthenticator(username, password); 

var request = new RestRequest("", Method.POST); 

// execute the request 
RestResponse response = client.Execute(request); 
var content = response.Content; // raw content as string 

阅读这篇文章的Web客户端,HttpClient的比较和HttpWebRequest的