黑马程序员技术交流社区
标题:
http类库
[打印本页]
作者:
sunrise2
时间:
2014-8-11 09:16
标题:
http类库
封装了
WEBCLIENT
和
HttpWebRequest
两中方法进行http的处理
当前只写了get post;且对于302的跳转是开启的
namespace webclient
{
class httpclient
{
private string cookie;
public string Cookie
{
get { return cookie; }
set { cookie = value; }
}
private string referer;
public string Referer
{
get { return referer; }
set { referer = value; }
}
/// <summary>
/// WEBCLIENT 方法处理http 当postdata为空 就使用GET,不为空就使用 POST
/// </summary>
/// <param name="url"></param>
/// <param name="postdata"></param>
/// <param name="type"></param>
/// <returns></returns>
public string webclient(string url, string postdata, string type)
{
string ret = "";
WebClient myClient = new WebClient();
myClient.Encoding = Encoding.GetEncoding(type);
myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myClient.Headers.Add("Cookie", cookie);
myClient.Headers.Add("Referer", referer);
if (postdata.Length == 0)
{
ret = myClient.DownloadString(url);
}
else
{
ret = myClient.UploadString(url, postdata);
}
if (cookie.Length == 0 )
{
cookie = myClient.ResponseHeaders.Get("Set-Cookie");
}
return ret;
}
/// <summary>
/// HttpWebRequest 方法处理http 当postdata为空 就使用GET,不为空就使用 POST
/// </summary>
/// <param name="url"></param>
/// <param name="postdata"></param>
/// <param name="type"></param>
/// <returns></returns>
public string HttpWebResponse(string url, string postdata, string type)
{
string ret = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.AllowAutoRedirect = true;
//myHttpWebRequest.co
/*----- */
string[] arrCookie = cookie.Split(';');
CookieContainer cookie_container = new CookieContainer(); //加载Cookie
foreach (string sCookie in arrCookie)
{
if (sCookie.IndexOf("expires") > 0)
continue;
cookie_container.SetCookies(new Uri(url), sCookie);
}
myHttpWebRequest.CookieContainer = cookie_container;
/*----- */
myHttpWebRequest.Referer = referer;
if (postdata.Length != 0)
{
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ContentLength = postdata.Length;
Stream newStream = myHttpWebRequest.GetRequestStream();
// Send the data.
byte[] byteArray = Encoding.GetEncoding(type).GetBytes(postdata);
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader str = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(type));
ret = str.ReadToEnd();
cookie = response.Headers.Get("Set-Cookie");
str.Close();
response.Close();
}
else
{
myHttpWebRequest.Method = "GET";
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding(type));
ret = sReader.ReadToEnd();
cookie = response.Headers.Get("Set-Cookie");
sReader.Close();
response.Close();
}
return ret;
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2