通过HttpWebRequest来进行POST请求的时候,如果发现在连续操作的情况下,会导致程序反应迟钝或被卡死,可以查看一下,是否是因为缺少了获取服务器状态这一关键步骤,所以才导致本地程序因为等待服务器的响应而暂停等待,正常完整的发送请求代码示例如下:

 

 1                 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(UrlText.Text + FileUpload1.FileName);
 2 
 3                 myReq.Method = "POST";
 4                 myReq.ContentType = "application/x-www-form-urlencoded";
 5                 myReq.ContentLength = FileUpload1.FileBytes.Length;
 6                 Stream outStream = myReq.GetRequestStream();
 7                 outStream.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length);
 8                 outStream.Close();
 9 
10                 // 获取服务器反馈结果
11                 using (HttpWebResponse response = (HttpWebResponse)myReq.GetResponse())
12                 {
13                     if (response.StatusCode != HttpStatusCode.OK)
14                     {
15                         throw new Exception("上传文件返回结果错误!");
16                     }

17                 }

    请注意看第10行及以下部分代码,就是获取请求响应的代码,而次过程为非常关键的步骤,不可缺少。

 

 

相关文章:

  • 2021-12-26
  • 2021-09-05
  • 2021-06-16
  • 2022-12-23
  • 2021-05-23
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
相关资源
相似解决方案