目前,我正在构建基于c#的桌面应用程序。它就像一个小型的网状汽车填充物。应用程序进行GET和POST调用以获取响应流中的图像。但是我的反应很慢。有关如何提高图像下载速度的任何建议。以下是下载图像和POST调用的代码
图像下载功能:
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;
}
}
}
POST请求发送功能:
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;
}