主要用两个方式进行Post HttpWebRespose 和WebClient post url及相关参数获取内容信息
WebClient 使用:
///
/// 多次调用Post请求返回 HTML信息 通过关一点通用
///
///
///
///
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 使用:
///
/// ASP 页面POST请求与获取结果
///
/// posturl
/// post参数
/// 页面编码
///
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)]*?>([\s\S].*?)")
.Cast().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList();
return resultStr[0].ToString();
}
///
/// ASP 页面Get请求与获取结果
///
/// posturl
/// post参数
/// 页面编码
///
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 使用
///
/// ASPX页面POST请求与获取结果
///
///
///
///
public static string HttpAspxPostMathHtml(string Url, List> 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)]*?>([\s\S].*?)")
.Cast().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");