• C# 实现http实现请求请求 HttpWebRequest


      1     public class XHttpRequest
      2     {
      3         //同步方式发起http get请求
      4         public string HttpGet(string url, string queryString)
      5         {
      6             string responseData = null;
      7 
      8             if (!string.IsNullOrEmpty(queryString))
      9             {
     10                 url += "?" + queryString;
     11             }
     12 
     13             HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
     14             webRequest.Method = "GET";
     15             webRequest.ServicePoint.Expect100Continue = false;
     16             webRequest.Timeout = 20000;
     17 
     18             StreamReader responseReader = null;
     19 
     20             try
     21             {
     22                 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
     23                 responseData = responseReader.ReadToEnd();
     24             }
     25             catch
     26             {
     27             }
     28             finally
     29             {
     30                 webRequest.GetResponse().GetResponseStream().Close();
     31                 responseReader.Close();
     32                 responseReader = null;
     33                 webRequest = null;
     34             }
     35 
     36             return responseData;
     37         }
     38 
     39         //同步方式发起http post请求
     40         public string HttpPost(string url, string queryString)
     41         {
     42             StreamWriter requestWriter = null;
     43             StreamReader responseReader = null;
     44 
     45             string responseData = null;
     46 
     47             HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
     48             webRequest.Method = "POST";
     49             webRequest.ContentType = "application/x-www-form-urlencoded";
     50             webRequest.ServicePoint.Expect100Continue = false;
     51             webRequest.Timeout = 20000;
     52 
     53             try
     54             {             
     55                 //POST the data.
     56                 requestWriter = new StreamWriter(webRequest.GetRequestStream());
     57                 requestWriter.Write(queryString);
     58                 requestWriter.Close();
     59                 requestWriter = null;
     60 
     61                 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
     62                 responseData = responseReader.ReadToEnd();
     63             }
     64             catch
     65             {
     66                 throw;
     67             }
     68             finally
     69             {
     70                 if (requestWriter != null)
     71                 {
     72                     requestWriter.Close();
     73                     requestWriter = null;
     74                 }
     75 
     76                 if (responseReader != null)
     77                 {
     78                     responseReader.Close();
     79                     responseReader = null;
     80                 }
     81 
     82                 webRequest.GetResponse().GetResponseStream().Close();
     83                 webRequest = null;
     84             }
     85 
     86             return responseData;
     87         }
     88 
     89         //同步方式发起http post请求,可以同时上传文件
     90         public string HttpPostWithFile(string url, string queryString, List<Parameter> files)
     91         {
     92             Stream requestStream = null;
     93             StreamReader responseReader = null;
     94             string responseData = null;
     95             string boundary = DateTime.Now.Ticks.ToString("x");
     96             url += '?' + queryString;
     97             HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
     98             webRequest.ServicePoint.Expect100Continue = false;
     99             webRequest.Timeout = 20000;
    100             webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
    101             webRequest.Method = "POST";
    102             webRequest.KeepAlive = true;
    103             webRequest.Credentials = CredentialCache.DefaultCredentials;
    104 
    105             try
    106             {
    107                 Stream memStream = new MemoryStream();
    108 
    109                 byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
    110                 string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
    111 
    112                 List<Parameter> listParams = HttpUtil.GetQueryParameters(queryString);
    113 
    114                 foreach (Parameter param in listParams)
    115                 {
    116                     string formitem = string.Format(formdataTemplate, param.Name, param.Value);
    117                     byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
    118                     memStream.Write(formitembytes, 0, formitembytes.Length);
    119                 }
    120 
    121                 memStream.Write(boundarybytes, 0, boundarybytes.Length);
    122 
    123                 string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: \"{2}\"\r\n\r\n";
    124 
    125                 foreach (Parameter param in files)
    126                 {
    127                     string name = param.Name;
    128                     string filePath = param.Value;
    129                     string file = Path.GetFileName(filePath);
    130                     string contentType = HttpUtil.GetContentType(file);
    131 
    132                     string header = string.Format(headerTemplate, name, file, contentType);
    133                     byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
    134 
    135                     memStream.Write(headerbytes, 0, headerbytes.Length);
    136 
    137                     FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    138                     byte[] buffer = new byte[1024];
    139                     int bytesRead = 0;
    140 
    141                     while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    142                     {
    143                         memStream.Write(buffer, 0, bytesRead);
    144                     }
    145 
    146                     memStream.Write(boundarybytes, 0, boundarybytes.Length);
    147                     fileStream.Close();
    148                 }
    149 
    150                 webRequest.ContentLength = memStream.Length;
    151 
    152                 requestStream = webRequest.GetRequestStream();
    153 
    154                 memStream.Position = 0;
    155                 byte[] tempBuffer = new byte[memStream.Length];
    156                 memStream.Read(tempBuffer, 0, tempBuffer.Length);
    157                 memStream.Close();
    158                 requestStream.Write(tempBuffer, 0, tempBuffer.Length);
    159                 requestStream.Close();
    160                 requestStream = null;
    161 
    162                 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
    163                 responseData = responseReader.ReadToEnd();
    164             }
    165             catch (Exception ex)
    166             {
    167             }
    168             finally
    169             {
    170                 if (requestStream != null)
    171                 {
    172                     requestStream.Close();
    173                     requestStream = null;
    174                 }
    175 
    176                 if (responseReader != null)
    177                 {
    178                     responseReader.Close();
    179                     responseReader = null;
    180                 }
    181 
    182                 webRequest.GetResponse().GetResponseStream().Close();
    183                 webRequest = null;
    184             }
    185 
    186             return responseData;
    187         }
    188     }
  • 相关阅读:
    指针与强制类型转换
    String 转Clob
    Oracle数据类型对应Java类型
    Oracle常见的问题
    面对批量更新之字符串的拼接
    java使用sigar 遇到问题的解决方案
    关于IE8以上 不引人css 症状
    java Springmvc ajax上传
    再也不要看到Eclipse万恶的arg0,arg1提示
    TextView实现跑马灯效果
  • 原文地址:https://www.cnblogs.com/CodeBase/p/2609826.html
Copyright © 2020-2023  润新知