C# 如何优化httpwebrequest调用性能
目前,我正在构建一个基于c#的桌面应用程序。它就像一个小的网页表单自动填充。应用程序在responsestream中进行GET和POST调用以获取图像。但我的反应很慢。关于如何提高图像下载速度的任何建议。以下是下载图像和POST通话的代码 图像下载功能:C# 如何优化httpwebrequest调用性能,c#,.net,web-services,optimization,httpwebrequest,C#,.net,Web Services,Optimization,Httpwebrequest,目前,我正在构建一个基于c#的桌面应用程序。它就像一个小的网页表单自动填充。应用程序在responsestream中进行GET和POST调用以获取图像。但我的反应很慢。关于如何提高图像下载速度的任何建议。以下是下载图像和POST通话的代码 图像下载功能: public void download_image(string go_to_url, string referer) { this.httpWebReques
public void download_image(string go_to_url, string referer)
{
this.httpWebRequest_cls_1 = null;
HttpWebResponse response = null;
try
{
this.httpWebRequest_cls_1 = (HttpWebRequest)WebRequest.Create(go_to_url);
this.httpWebRequest_cls_1.Proxy = null;
WebRequest.DefaultWebProxy = null;
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_cls_1.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_cls_1.ServicePoint.Expect100Continue = false;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLimit = 100000;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLeaseTimeout = 65000;
this.httpWebRequest_cls_1.ServicePoint.MaxIdleTime = 100000;
this.httpWebRequest_cls_1.CookieContainer = global_store.cookieContainer_0;
this.httpWebRequest_cls_1.Referer = str1;
this.httpWebRequest_cls_1.KeepAlive = true;
this.httpWebRequest_cls_1.ReadWriteTimeout = 0xc350;
this.httpWebRequest_cls_1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
this.httpWebRequest_cls_1.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
this.httpWebRequest_cls_1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
this.httpWebRequest_cls_1.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_cls_1.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_cls_1.PreAuthenticate = true;
this.httpWebRequest_cls_1.AllowWriteStreamBuffering = false;
this.httpWebRequest_cls_1.Pipelined = false;
this.httpWebRequest_cls_1.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_cls_1.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_cls_1.Headers.Set("Cache-Control", "max-age=86400");
this.httpWebRequest_cls_1.ContentLength = 0L;
this.httpWebRequest_cls_1.Method = "GET";
response = (HttpWebResponse)this.httpWebRequest_cls_1.GetResponse();
global_store.image_0 = Image.FromStream(response.GetResponseStream());
staticstore.get_cap_time = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute, DateTime.Now.Second);
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_cls_1 != null)
{
this.httpWebRequest_cls_1.Abort();
this.httpWebRequest_cls_1 = null;
}
}
}
public string simple_web_call(string go_to, string from, string params_to_post, string conditions)
{
string str = "";
string responseHeader = "";
httpWebRequest_3 = null;
Stream requestStream = null;
HttpWebResponse response = null;
Stream responseStream = null;
try
{
this.httpWebRequest_3 = (HttpWebRequest)WebRequest.Create(new Uri(go_to));
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_3.CachePolicy = policy;
this.httpWebRequest_3.Proxy = null;
WebRequest.DefaultWebProxy = null;
this.httpWebRequest_3.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_3.ServicePoint.Expect100Continue = false;
this.httpWebRequest_3.ServicePoint.ConnectionLimit = 65000;
this.httpWebRequest_3.ServicePoint.ConnectionLeaseTimeout = Class9.int_7;
this.httpWebRequest_3.ServicePoint.MaxIdleTime = 10000;
this.httpWebRequest_3.CookieContainer = Class28.cookieContainer_0;
this.httpWebRequest_3.Referer = from;
this.httpWebRequest_3.KeepAlive = true;
this.httpWebRequest_3.Connection = "keepalive";
this.httpWebRequest_3.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
this.httpWebRequest_3.UserAgent = "Mozilla/5.0 (Windows T 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
this.httpWebRequest_3.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_3.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_3.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
if (params_to_post != "")
{
this.httpWebRequest_3.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(params_to_post);
this.httpWebRequest_3.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_3.ContentLength = bytes.Length;
requestStream = this.httpWebRequest_3.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
}
else
{
this.httpWebRequest_3.Method = "GET";
}
response = (HttpWebResponse)this.httpWebRequest_3.GetResponse();
HttpWebResponse response2 = response;
switch (response2.StatusCode)
{
case HttpStatusCode.OK:
{
responseStream = response2.GetResponseStream();
str = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
responseStream.Close();
responseStream = null;
goto flush_all;
}
case HttpStatusCode.MovedPermanently:
case HttpStatusCode.Found:
case HttpStatusCode.SeeOther:
case HttpStatusCode.TemporaryRedirect:
responseHeader = response2.GetResponseHeader("Location");
if (!responseHeader.Contains("err")) { break; }
str = "retry";
goto flush_all;
default:
str = "retry";
goto flush_all;
}
str = responseHeader;
flush_all:
response2 = null;
response.Close();
response = null;
this.httpWebRequest_3 = null;
if (str == "") { str = "retry"; }
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (requestStream != null)
{
requestStream.Close();
requestStream.Dispose();
requestStream = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_3 != null)
{
this.httpWebRequest_3.Abort();
this.httpWebRequest_3 = null;
}
}
return str;
}
请求发送后功能:
public void download_image(string go_to_url, string referer)
{
this.httpWebRequest_cls_1 = null;
HttpWebResponse response = null;
try
{
this.httpWebRequest_cls_1 = (HttpWebRequest)WebRequest.Create(go_to_url);
this.httpWebRequest_cls_1.Proxy = null;
WebRequest.DefaultWebProxy = null;
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_cls_1.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_cls_1.ServicePoint.Expect100Continue = false;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLimit = 100000;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLeaseTimeout = 65000;
this.httpWebRequest_cls_1.ServicePoint.MaxIdleTime = 100000;
this.httpWebRequest_cls_1.CookieContainer = global_store.cookieContainer_0;
this.httpWebRequest_cls_1.Referer = str1;
this.httpWebRequest_cls_1.KeepAlive = true;
this.httpWebRequest_cls_1.ReadWriteTimeout = 0xc350;
this.httpWebRequest_cls_1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
this.httpWebRequest_cls_1.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
this.httpWebRequest_cls_1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
this.httpWebRequest_cls_1.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_cls_1.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_cls_1.PreAuthenticate = true;
this.httpWebRequest_cls_1.AllowWriteStreamBuffering = false;
this.httpWebRequest_cls_1.Pipelined = false;
this.httpWebRequest_cls_1.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_cls_1.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_cls_1.Headers.Set("Cache-Control", "max-age=86400");
this.httpWebRequest_cls_1.ContentLength = 0L;
this.httpWebRequest_cls_1.Method = "GET";
response = (HttpWebResponse)this.httpWebRequest_cls_1.GetResponse();
global_store.image_0 = Image.FromStream(response.GetResponseStream());
staticstore.get_cap_time = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute, DateTime.Now.Second);
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_cls_1 != null)
{
this.httpWebRequest_cls_1.Abort();
this.httpWebRequest_cls_1 = null;
}
}
}
public string simple_web_call(string go_to, string from, string params_to_post, string conditions)
{
string str = "";
string responseHeader = "";
httpWebRequest_3 = null;
Stream requestStream = null;
HttpWebResponse response = null;
Stream responseStream = null;
try
{
this.httpWebRequest_3 = (HttpWebRequest)WebRequest.Create(new Uri(go_to));
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_3.CachePolicy = policy;
this.httpWebRequest_3.Proxy = null;
WebRequest.DefaultWebProxy = null;
this.httpWebRequest_3.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_3.ServicePoint.Expect100Continue = false;
this.httpWebRequest_3.ServicePoint.ConnectionLimit = 65000;
this.httpWebRequest_3.ServicePoint.ConnectionLeaseTimeout = Class9.int_7;
this.httpWebRequest_3.ServicePoint.MaxIdleTime = 10000;
this.httpWebRequest_3.CookieContainer = Class28.cookieContainer_0;
this.httpWebRequest_3.Referer = from;
this.httpWebRequest_3.KeepAlive = true;
this.httpWebRequest_3.Connection = "keepalive";
this.httpWebRequest_3.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
this.httpWebRequest_3.UserAgent = "Mozilla/5.0 (Windows T 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
this.httpWebRequest_3.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_3.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_3.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
if (params_to_post != "")
{
this.httpWebRequest_3.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(params_to_post);
this.httpWebRequest_3.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_3.ContentLength = bytes.Length;
requestStream = this.httpWebRequest_3.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
}
else
{
this.httpWebRequest_3.Method = "GET";
}
response = (HttpWebResponse)this.httpWebRequest_3.GetResponse();
HttpWebResponse response2 = response;
switch (response2.StatusCode)
{
case HttpStatusCode.OK:
{
responseStream = response2.GetResponseStream();
str = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
responseStream.Close();
responseStream = null;
goto flush_all;
}
case HttpStatusCode.MovedPermanently:
case HttpStatusCode.Found:
case HttpStatusCode.SeeOther:
case HttpStatusCode.TemporaryRedirect:
responseHeader = response2.GetResponseHeader("Location");
if (!responseHeader.Contains("err")) { break; }
str = "retry";
goto flush_all;
default:
str = "retry";
goto flush_all;
}
str = responseHeader;
flush_all:
response2 = null;
response.Close();
response = null;
this.httpWebRequest_3 = null;
if (str == "") { str = "retry"; }
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (requestStream != null)
{
requestStream.Close();
requestStream.Dispose();
requestStream = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_3 != null)
{
this.httpWebRequest_3.Abort();
this.httpWebRequest_3 = null;
}
}
return str;
}
提供更多的源代码,这还不够。是否要我添加应用程序的所有源代码?应用程序的主代码驻留在上面两个函数中。哈哈,不。这很讽刺。你可以用它来查看客户端和服务器之间的实际计时和响应。我希望服务器响应速度较慢。提供更多源代码,这还不够。是否要我添加应用程序的所有源代码?应用程序的主代码驻留在上面两个函数中。哈哈,不。这很讽刺。你可以用它来查看客户端和服务器之间的实际计时和响应。我希望服务器响应缓慢。