C# Get和Post获取和请求页面信息(HttpWebRequest 、WebClient、HttpClient)
2018-03-12 20:11
316 查看
主要用两个方式进行Post HttpWebRespose 和WebClient post url及相关参数获取内容信息
WebClient 使用:
/// <summary> /// 多次调用Post请求返回 HTML信息 通过关一点通用 /// </summary> /// <param name="url"></param> /// <param name="postString"></param> /// <returns></returns> public static string HttpAspxPostHtmlInfo(string url, string postString) { byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,事先要看下抓取网页的编码方式 WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流 var retString = Encoding.UTF8.GetString(responseData);//解码返回请求的html 内容 return retString; }HttpWebReqest 使用:
/// <summary> /// ASP 页面POST请求与获取结果 /// </summary> /// <param name="Url">posturl</param> /// <param name="postDataStr">post参数</param> /// <param name="encoding">页面编码</param> /// <returns></returns> public static string HttpAspPostMathHtml(string Url, string postDataStr, string encoding) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, image/jxr, */*"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; request.Headers.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"; byte[] bytes = System.Text.Encoding.Default.GetBytes(postDataStr); request.ContentLength = bytes.Length; Stream stream = request.GetRequestStream(); stream.Write(bytes, 0, bytes.Length); stream.Close();//以上是POST数据的写入 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var retString = string.Empty; using (Stream responsestream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.GetEncoding(encoding))) { retString = sr.ReadToEnd(); } } var resultStr = Regex.Matches(DelHTML(retString), @"(?is)<body[^>]*?>([\s\S].*?)</body>") .Cast<Match>().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList(); return resultStr[0].ToString(); } /// <summary> /// ASP 页面Get请求与获取结果 /// </summary> /// <param name="Url">posturl</param> /// <param name="postDataStr">post参数</param> /// <param name="encoding">页面编码</param> /// <returns></returns> public static string HttpAspGetMathHtml(string Url, string postDataStr, string encoding) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + "?" + postDataStr); request.Method = "Get"; request.Accept = "text/html, application/xhtml+xml, image/jxr, */*"; request.KeepAlive = true; request.Headers.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var retString = string.Empty; using (Stream responsestream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.GetEncoding(encoding))) { retString = sr.ReadToEnd(); } } return retString; }HttpClient 使用
/// <summary> /// ASPX页面POST请求与获取结果 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public static string HttpAspxPostMathHtml(string Url, List<KeyValuePair<String, String>> postDataStr) { var retString = string.Empty; HttpClient httpClient = new HttpClient(); HttpResponseMessage response = httpClient.GetAsync(new Uri(Url)).Result; response = httpClient.PostAsync(new Uri(Url), new FormUrlEncodedContent(postDataStr)).Result; retString = response.Content.ReadAsStringAsync().Result; var resultStr = Regex.Matches(DelHTML(retString), @"(?is)<body[^>]*?>([\s\S].*?)</body>") .Cast<Match>().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList(); //用完要记得释放 httpClient.Dispose(); return resultStr[0].ToString(); }
HttpAspPostMathHtml 使用方法:
var sPuci = HttpAspPostMathHtml("http://www.xx.com/search/index_dt_container.asp", "search=true&companyname= &companycode=&container_no=&bill_no=" + strBlNo + "&btn3.x=39&btn3.y=15", "gb2312");HttpAspxPostHtmlInfo使用方法:
HttpAspxPostHtmlInfo("http://www.xx.com/search/index_dt_container.asp", "search=true&companyname= &companycode=&container_no=&bill_no=" + strBlNo + "&btn3.x=39&btn3.y=15");HttpAspGetMathHtml使用方法:
HttpAspGetMathHtml("http://2.22.96.22/query/search_bl_no.asp", "BL_NO1=" + strBlNo + "&submit1=%B2%E9%D1%AF", "gb2312");
相关文章推荐
- C# Get请求数据 WebClient和HttpWebRequest获取HTML代码
- HttpWebRequest post请求获取webservice void数据信息
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#使用HttpWebRequest POST或GET信息
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- c# HttpWebRequest post和get请求http
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- 转:C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#POST数据,HttpWebRequest请求页面,HttpWebResponse返回数据
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- C# http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- C# HttpWebRequest Post Get 请求数据