ASP.NET模拟http进行GET/POST请求

匿名 (未验证) 提交于 2019-12-03 00:15:02

备忘录:C#请求外部接口的两种POST方式。

1. 通过FormBody参数形式请求:

        /// 发送请求           /// </summary>           /// <param name="url">请求地址</param>           /// <param name="sendData">参数格式 “name=张三&pass=123456”</param>           /// <returns></returns>           public static string RequestWebAPI(string url, string sendData)         {             string backMsg = "";             try             {                 System.Net.WebRequest httpRquest = System.Net.HttpWebRequest.Create(url);                 httpRquest.Method = "POST";                 //这行代码很关键,不设置ContentType将导致后台参数获取不到值                   httpRquest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";                 byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(sendData);                 //httpRquest.ContentLength = dataArray.Length;                   System.IO.Stream requestStream = null;                 if (string.IsNullOrWhiteSpace(sendData) == false)                 {                     requestStream = httpRquest.GetRequestStream();                     requestStream.Write(dataArray, 0, dataArray.Length);                     requestStream.Close();                 }                 System.Net.WebResponse response = httpRquest.GetResponse();                 System.IO.Stream responseStream = response.GetResponseStream();                 System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);                 backMsg = reader.ReadToEnd();                   reader.Close();                 reader.Dispose();                   requestStream.Dispose();                 responseStream.Close();                 responseStream.Dispose();             }             catch (Exception)             {                 throw;             }             return backMsg;         }  

2. 通过json参数形式请求:

    public string GetPage(string posturl, string postData)     {         Stream outstream = null;         Stream instream = null;         StreamReader sr = null;         HttpWebResponse response = null;         HttpWebRequest request = null;         Encoding encoding = Encoding.UTF8;         byte[] data = encoding.GetBytes(postData);         try         {             // 设置参数             request = WebRequest.Create(posturl) as HttpWebRequest;             CookieContainer cookieContainer = new CookieContainer();             request.CookieContainer = cookieContainer;             request.AllowAutoRedirect = true;             request.Method = "POST";             request.ContentType = "application/json";             request.ContentLength = data.Length;             outstream = request.GetRequestStream();             outstream.Write(data, 0, data.Length);             outstream.Close();             //发送请求并获取相应回应数据             response = request.GetResponse() as HttpWebResponse;             //直到request.GetResponse()程序才开始向目标网页发送Post请求             instream = response.GetResponseStream();             sr = new StreamReader(instream, encoding);             //返回结果网页(html)代码             string content = sr.ReadToEnd();             string err = string.Empty;             //   Response.Write(content);             return content;         }         catch (Exception ex)         {             string err = ex.Message;             return string.Empty;         }     }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!