C# 为什么它要提出两次请求

C# 为什么它要提出两次请求,c#,asp.net,http,httpwebrequest,C#,Asp.net,Http,Httpwebrequest,编写此代码是为了向服务发送请求并接收简单的json响应,但该服务的服务器日志显示请求两次,延迟为1秒。我不明白为什么这段代码会发出两次请求。如果缺少一些配置,请指导我 string StrUrl = @"http://www.myapp.com/Tracker.json"; Uri uri1 = new Uri(StrUrl); HttpWebRequest webRequest1 = (HttpWebRequest)HttpWebRequest.Create(uri1); webRequest

编写此代码是为了向服务发送请求并接收简单的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();
这些可能是类似的问题


通过使用解决了此问题


阅读本文,比较WebClient、HttpClient和HttpWebRequest的优劣。也许你调用了两次函数?检查你的代码,看看你是否调用了两次函数。我在调试器中用断点测试了代码。它仅在服务的服务器在centos 6上运行tomcat时运行。可能存在某种兼容性问题?检查是否使用了基于401质询的身份验证,因为这将记录两个请求,并且确实发送了两个请求。
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