发送GET/POST请求
C# 2018-07-02 10:53:06

1、添加 工具类Tool

C/C++ Code复制内容到剪贴板
  1. //引入的命名空间  
  2. using System;  
  3. using System.Collections.Generic;  //使用Dictionary集合  
  4. using System.Web;                  // 使用HttpUtility  
  5. using System.Net;                  // 使用HttpWebRequest  
  6. using System.IO;                   // 使用stream  
  7. using System.Text;                 // 使用StringBuilder  
  8. //using System.Linq;  
  9. //using System.Threading.Tasks;  
  10. //using System.Configuration;  
  11. //using System.Data.SqlClient;  
  12. //using System.Data;  
  13. //using System.Diagnostics;  
  14. //using System.Data.SqlClient;      //用于SQL Sever数据访问的命名空间  
  15. //using System.Data;               //DataSet类的命名空间  
  16. //using System.Windows.Forms;      //DataGridView控件类的命名空间  
  17.   
  18. namespace WindowsFormsApplication1  
  19. {  
  20.     class Tools  
  21.     {  
  22.   
  23.         public static string test()  
  24.         {  
  25.             return "test";  
  26.         }  
  27.   
  28.   
  29.         /// <summary>  
  30.         /// Http (GET/POST)  
  31.         /// </summary>  
  32.         /// <param name="url">请求URL</param>  
  33.         /// <param name="parameters">请求参数</param>  
  34.         /// <param name="method">请求方法</param>  
  35.         /// <returns>响应内容</returns>  
  36.         public static string HttpRequest(string url, IDictionary<string, string> parameters, string method)  
  37.         {  
  38.               
  39.             if (method.ToLower() == "post")  
  40.             {  
  41.                 HttpWebRequest req = null;  
  42.                 HttpWebResponse rsp = null;  
  43.                 System.IO.Stream reqStream = null;  
  44.                 try  
  45.                 {  
  46.                     req = (HttpWebRequest)WebRequest.Create(url);  
  47.                     req.Method = method;  
  48.                     req.KeepAlive = false;  
  49.                     req.ProtocolVersion = HttpVersion.Version10;  
  50.                     req.Timeout = 5000;  
  51.                     req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";  
  52.                     byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));  
  53.                     reqStream = req.GetRequestStream();  
  54.                     reqStream.Write(postData, 0, postData.Length);  
  55.                     rsp = (HttpWebResponse)req.GetResponse();  
  56.                     Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);  
  57.   
  58.                     return GetResponseAsString(rsp, encoding);  
  59.                 }  
  60.                 catch (Exception ex)  
  61.                 {  
  62.                     return ex.Message;  
  63.                 }  
  64.                 finally  
  65.                 {  
  66.                     if (reqStream != null) reqStream.Close();  
  67.                     if (rsp != null) rsp.Close();  
  68.                 }  
  69.             }  
  70.             else  
  71.             {  
  72.                 //创建请求  
  73.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));  
  74.   
  75.                 //return url + "?" + BuildQuery(parameters, "utf8");  
  76.   
  77.                 //GET请求  
  78.                 request.Method = "GET";  
  79.                 request.ReadWriteTimeout = 5000;  
  80.                 request.ContentType = "text/html;charset=UTF-8";  
  81.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  82.                 Stream myResponseStream = response.GetResponseStream();  
  83.                 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));  
  84.   
  85.                 //返回内容  
  86.                 string retString = myStreamReader.ReadToEnd();  
  87.                 return retString;  
  88.             }  
  89.         }  
  90.         /// <summary>  
  91.         /// 组装普通文本请求参数。  
  92.         /// </summary>  
  93.         /// <param name="parameters">Key-Value形式请求参数字典</param>  
  94.         /// <returns>URL编码后的请求数据</returns>  
  95.         static string BuildQuery(IDictionary<string, string> parameters, string encode)  
  96.         {  
  97.             StringBuilder postData = new StringBuilder();  
  98.             bool hasParam = false;  
  99.             IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();  
  100.             while (dem.MoveNext())  
  101.             {  
  102.                 string name = dem.Current.Key;  
  103.                 string value = dem.Current.Value;  
  104.                 // 忽略参数名或参数值为空的参数  
  105.                 if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)  
  106.                 {  
  107.                     if (hasParam)  
  108.                     {  
  109.                         postData.Append("&");  
  110.                     }  
  111.                     postData.Append(name);  
  112.                     postData.Append("=");  
  113.                     if (encode == "gb2312")  
  114.                     {  
  115.                         postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));  
  116.                     }  
  117.                     else if (encode == "utf8")  
  118.                     {  
  119.                         postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));  
  120.                     }  
  121.                     else  
  122.                     {  
  123.                         postData.Append(value);  
  124.                     }  
  125.                     hasParam = true;  
  126.                 }  
  127.             }  
  128.             return postData.ToString();  
  129.         }  
  130.         /// <summary>  
  131.         /// 把响应流转换为文本。  
  132.         /// </summary>  
  133.         /// <param name="rsp">响应流对象</param>  
  134.         /// <param name="encoding">编码方式</param>  
  135.         /// <returns>响应文本</returns>  
  136.         static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)  
  137.         {  
  138.             System.IO.Stream stream = null;  
  139.             StreamReader reader = null;  
  140.             try  
  141.             {  
  142.                 // 以字符流的方式读取HTTP响应  
  143.                 stream = rsp.GetResponseStream();  
  144.                 reader = new StreamReader(stream, encoding);  
  145.                 return reader.ReadToEnd();  
  146.             }  
  147.             finally  
  148.             {  
  149.                 // 释放资源  
  150.                 if (reader != null) reader.Close();  
  151.                 if (stream != null) stream.Close();  
  152.                 if (rsp != null) rsp.Close();  
  153.             }  
  154.         }  
  155.   
  156.     }  
  157. }  

 

使用:

C/C++ Code复制内容到剪贴板
  1. //声明并添加元素  
  2. Dictionary<String, String> PostData = new Dictionary<String, String>();  
  3. String URL = "http://xxx1.4:9507/";  
  4. PostData.Add("act""licensePlate");  
  5.   
  6. // var tools = new Tools();  
  7. string result = Tools.HttpRequest(URL, PostData, "post");  
  8. Console.ReadLine();  

 

 

 

本文来自于:http://www.yoyo88.cn/study/net/329.html

Powered by yoyo苏ICP备15045725号