#region 内部使用 HTTP GET模式提交数据 【HttpPostData】
/// <summary>
/// 内部使用 HTTP GET模式提交数据 【HttpPostData】
/// </summary>
/// <param name="url">完整的URL地址</param>+
/// <param name="requestBytes">提交参数</param>
/// <returns>返回数据</returns>
private string HttpPostData(string url, string data)
{
try
{
Stream newStream = null;
WebRequest myReq = WebRequest.Create(url);
byte[] byte1 = UTF8Encoding.Default.GetBytes(data);
myReq.Method = "POST";
myReq.ContentType = "application/x-www-form-urlencoded";
myReq.ContentLength = byte1.Length;
newStream = myReq.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
WebResponse myResp = myReq.GetResponse();
Stream resStream = myResp.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.GetEncoding("UTF-8"));
return sr.ReadToEnd();
}
catch (Exception ex)
{
LogHelper.WriteException("【HttpPostData】异常url【" + url + "】", ex);
return "";
}
}
#endregion
#region 内部使用 HTTP GET模式提交数据 【HttpGetData】
/// <summary>
/// 内部使用 HTTP GET模式提交数据
/// 如:http://localhost:8080/pastf.aspx?username=asb&passpod=123;
/// </summary>
/// <param name="url">提交地址(需要加参数 用小写)</param>
/// <returns></returns>
private string HttpGetData(string url)
{
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string page;
try
{
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Proxy = System.Net.WebProxy.GetDefaultProxy();
//allow auto redirects from redirect headers
request.AllowAutoRedirect = true;
//maximum of 10 auto redirects
request.MaximumAutomaticRedirections = 10;
//30 second timeout for request
request.Timeout = (int)new TimeSpan(0, 3, 0).TotalMilliseconds;
//give the crawler a name.
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
//request.UserAgent="Mozilla/3.0 (compatible; My Browser/1.0)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
page = readStream.ReadToEnd();
}
catch (Exception ex)
{
LogHelper.WriteException("【HttpGetData】异常url【" + url + "】", ex);
page = "";
}
return page;
}
#endregion