c#通过GET/POST获取页面的代码
c#通过GET/POST获取页面的代码
001 |
/// <summary> |
002 |
/// 提供web处理方法的类 |
003 |
/// </summary> |
004 |
public class WebTreatment |
005 |
{ |
006 |
/// <summary> |
007 |
/// 通过GET方式获取页面的方法 |
008 |
/// </summary> |
009 |
/// <param name="urlString">请求的URL</param> |
010 |
/// <param name="encoding">页面编码</param> |
011 |
/// <returns></returns> |
012 |
public static string GetHtmlFromGet( string urlString, Encoding encoding) |
013 |
{ |
014 |
//定义局部变量 |
015 |
HttpWebRequest httpWebRequest = null ; |
016 |
HttpWebResponse httpWebRespones = null ; |
017 |
Stream stream = null ; |
018 |
string htmlString = string .Empty; |
019 |
|
020 |
//请求页面 |
021 |
try |
022 |
{ |
023 |
httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest; |
024 |
} |
025 |
//处理异常 |
026 |
catch (Exception ex) |
027 |
{ |
028 |
throw new Exception( "建立页面请求时发生错误!" , ex); |
029 |
} |
030 |
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)" ; |
031 |
//获取服务器的返回信息 |
032 |
try |
033 |
{ |
034 |
httpWebRespones = (HttpWebResponse)httpWebRequest.GetResponse(); |
035 |
stream = httpWebRespones.GetResponseStream(); |
036 |
} |
037 |
//处理异常 |
038 |
catch (Exception ex) |
039 |
{ |
040 |
throw new Exception( "接受服务器返回页面时发生错误!" , ex); |
041 |
} |
042 |
StreamReader streamReader = new StreamReader(stream,encoding); |
043 |
//读取返回页面 |
044 |
try |
045 |
{ |
046 |
htmlString = streamReader.ReadToEnd(); |
047 |
} |
048 |
//处理异常 |
049 |
catch (Exception ex) |
050 |
{ |
051 |
throw new Exception( "读取页面数据时发生错误!" , ex); |
052 |
} |
053 |
//释放资源返回结果 |
054 |
streamReader.Close(); |
055 |
stream.Close(); |
056 |
return htmlString; |
057 |
} |
058 |
|
059 |
/// <summary> |
060 |
/// 提供通过POST方法获取页面的方法 |
061 |
/// </summary> |
062 |
/// <param name="urlString">请求的URL</param> |
063 |
/// <param name="encoding">页面使用的编码</param> |
064 |
/// <param name="postDataString">POST数据</param> |
065 |
/// <returns>获取的页面</returns> |
066 |
public static string GetHtmlFromPost( string urlString, Encoding encoding, string postDataString) |
067 |
{ |
068 |
//定义局部变量 |
069 |
CookieContainer cookieContainer = new CookieContainer(); |
070 |
HttpWebRequest httpWebRequest = null ; |
071 |
HttpWebResponse httpWebResponse = null ; |
072 |
Stream inputStream = null ; |
073 |
Stream outputStream = null ; |
074 |
StreamReader streamReader = null ; |
075 |
string htmlString = string .Empty; |
076 |
//转换POST数据 |
077 |
byte [] postDataByte = encoding.GetBytes(postDataString); |
078 |
//建立页面请求 |
079 |
try |
080 |
{ |
081 |
httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest; |
082 |
} |
083 |
//处理异常 |
084 |
catch (Exception ex) |
085 |
{ |
086 |
throw new Exception( "建立页面请求时发生错误!" , ex); |
087 |
} |
088 |
//指定请求处理方式 |
089 |
httpWebRequest.Method = "POST" ; |
090 |
httpWebRequest.KeepAlive = false ; |
091 |
httpWebRequest.ContentType = "application/x-www-form-urlencoded" ; |
092 |
httpWebRequest.CookieContainer = cookieContainer; |
093 |
httpWebRequest.ContentLength = postDataByte.Length; |
094 |
//向服务器传送数据 |
095 |
try |
096 |
{ |
097 |
inputStream = httpWebRequest.GetRequestStream(); |
098 |
inputStream.Write(postDataByte, 0, postDataByte.Length); |
099 |
} |
100 |
//处理异常 |
101 |
catch (Exception ex) |
102 |
{ |
103 |
throw new Exception( "发送POST数据时发生错误!" , ex); |
104 |
} |
105 |
finally |
106 |
{ |
107 |
inputStream.Close(); |
108 |
} |
109 |
//接受服务器返回信息 |
110 |
try |
111 |
{ |
112 |
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; |
113 |
outputStream = httpWebResponse.GetResponseStream(); |
114 |
streamReader = new StreamReader(outputStream, encoding); |
115 |
htmlString = streamReader.ReadToEnd(); |
116 |
} |
117 |
//处理异常 |
118 |
catch (Exception ex) |
119 |
{ |
120 |
throw new Exception( "接受服务器返回页面时发生错误!" , ex); |
121 |
} |
122 |
finally |
123 |
{ |
124 |
streamReader.Close(); |
125 |
} |
126 |
foreach (Cookie cookie in httpWebResponse.Cookies) |
127 |
{ |
128 |
cookieContainer.Add(cookie); |
129 |
} |
130 |
return htmlString; |
131 |
} |
132 |
} |