首页 新闻 会员 周边 捐助

异步的httpwebrequest怎么写,我的测试都不通过帮看下

0
[待解决问题]

#region testget
  void GetResponseCallback(IAsyncResult ar)
  {
  try
  {
  HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
  //HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

  HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
  InitResponseText();

  if (IsImg)
  {
  Image img;
  img = new System.Drawing.Bitmap(response.GetResponseStream());
  img.Save(ImgName);
  img.Dispose();
  }
  else
  {
  Stream resStream = response.GetResponseStream();
  Encoding encode = GetEncoding;
  StreamReader readStream = new StreamReader(resStream, encode);

  HtmlText = readStream.ReadToEnd();

  for (int i = 0; i < response.Headers.Count; i++)
  HeaderText += response.Headers.GetKey(i) + ": " + response.Headers[i] + "\r\n";

  if (GetCookieMode)
  {
  for (int i = 0; i < response.Cookies.Count; i++)
  CookieText += response.Cookies[i].Name + "=" + response.Cookies[i].Value + ";";
  }

  resStream.Close();
  readStream.Close();

  resStream.Dispose();
  readStream.Dispose();
  }

  }
  catch (Exception ex) { Console.WriteLine(ex); }
  }

   
  public void HttpGet()
  {
  try
  {
  Uri uri = new Uri(URLData);
  ServicePointManager.DefaultConnectionLimit = ConnectLimit;
  ServicePointManager.UseNagleAlgorithm = false;
  ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

  request.UnsafeAuthenticatedConnectionSharing = true;
  request.ServicePoint.ConnectionLimit = ConnectLimit;
  request.ServicePoint.Expect100Continue = false;
  request.ServicePoint.UseNagleAlgorithm = false;

  request.Method = "GET";
  request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, */*";

  if (RefData != "")
  {
  request.Referer = RefData;
  }
  request.Headers.Add("Accept-Language", "zh-cn");
  request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)";
  request.Headers.Add("Expires", "0");
  request.AllowAutoRedirect = AutoRedirect;

  request.AutomaticDecompression = DecompressionMethods.GZip;
  request.AllowWriteStreamBuffering = false;
  request.Timeout = TimeOut;
  request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
  request.KeepAlive = true;

  if (GetCookieMode)
  {
  request.CookieContainer = new CookieContainer();
  if (CookieData != "")
  {
  string[] tmpCookie = CookieData.Split(';');
  for (int i = 0; i < tmpCookie.Length; i++)
  if (tmpCookie[i] != "")
  {
  string[] setCookie = tmpCookie[i].Split(new Char[] { '=' }, 2);
  request.CookieContainer.Add(uri, new Cookie(setCookie[0], setCookie[1]));
  }
  }
  }
  else
  {
  if (CookieData != "")
  request.Headers.Add("Cookie", CookieData);
  }


  HttpWebResponse response = (HttpWebResponse)request.BeginGetResponse(GetResponseCallback, request);
  allDone.WaitOne();

  }
  catch (Exception ee)
  {
  HtmlText = ee.Message;// "Error";
  }
  }

#endregion

问题补充: 说的错误是 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);这一个位置 {System.Net.WebException: 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF 在 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 SuperHorse.MyClass.CHTTP.GetResponseCallback(IAsyncResult ar) 位置 E:\work\code\SuperHorse\MyClass\MyFunction.cs:行号 528}
superstar的主页 superstar | 菜鸟二级 | 园豆:261
提问于:2011-03-29 09:59
< >
分享
所有回答(1)
0
写代码的小2B | 园豆:4377 (老鸟四级) | 2011-03-29 10:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册