C# 模拟web get请求、post请求
get请求:
post请求:
#region 模拟Post提交 /// <summary> /// 通过POST方式发送数据 /// </summary> /// <param name="url">请求URL</param> /// <param name="json">请求参数</param> /// <returns></returns> public static string HttpPost(string url, string strXML) { try { Encoding encoding = Encoding.GetEncoding("UTF-8"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); byte[] buffer; buffer = encoding.GetBytes(strXML); request.Method = "Post"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER"; request.ContentType = "application/json; charset=UTF-8";//application/x-www-form-urlencoded;charset=UTF-8 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.Referer = "http://www.aramex.com/express/track-results.aspx"; request.ContentLength = buffer.Length; Stream postStream = request.GetRequestStream(); postStream.Write(buffer, 0, buffer.Length); postStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //返回信息 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string strResponse = reader.ReadToEnd(); reader.Close(); response.Close(); return strResponse; } catch (Exception ex) { return ex.Message; } } #endregion温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70010.html