110,556
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace WebLogin {
class WebServerClass {
public static HttpWebResponse CreateRequest(string url, string postData, CookieCollection cookies) {
Uri urls = new Uri(url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
myRequest.KeepAlive = true;
if(cookies != null) {
foreach(Cookie cook in cookies) {
cook.Domain = urls.Host;
myRequest.CookieContainer.Add(cook);
}
}
if(postData != null) {
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
return myResponse;
}
public static HttpWebResponse CreateRequest(string url) {
return CreateRequest(url, null, null);
}
}
}