我新的編程(C#)和我開始寫的MMORPG學習與編程網站和東西通信的殭屍...登錄到使用網站的HttpWebRequest
我發現有關此主題的許多問題在這裏和那裏。最後一個伸出援助之手,我能找到的東西,並完成它,而不幸的是不工作:(
的mothods:
public static CookieCollection GetCookie(HttpWebRequest request)
{
if (request.CookieContainer == null)
{ return new CookieContainer().GetCookies(request.RequestUri); }
else
{ return request.CookieContainer.GetCookies(request.RequestUri); }
}
public static CookieContainer GetCookie(HttpWebResponse response)
{
CookieContainer cookiecontainer = new CookieContainer();
cookiecontainer.Add(response.Cookies);
return cookiecontainer;
}
public static void SetCookie(HttpWebRequest request, CookieContainer cookie)
{
request.CookieContainer = cookie;
}
public static void SetCookie(HttpWebResponse response, CookieCollection cookie)
{
response.Cookies = cookie;
}
public static HttpWebResponse PostData(string uri,string request,CookieContainer cookie)
{
HttpWebRequest httprequest;
byte[] requestbytes;
Stream requeststream;
HttpWebResponse httpresponse;
httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
if (cookie == null)
{httprequest.CookieContainer=new CookieContainer();}
else
{httprequest.CookieContainer=cookie;}
httprequest.Method = "POST";
httprequest.ContentType = "application/x-www-form-urlencoded";
httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
requestbytes = ASCIIEncoding.ASCII.GetBytes(request);
httprequest.ContentLength = requestbytes.Length;
requeststream=httprequest.GetRequestStream();
requeststream.Write(requestbytes,0,requestbytes.Length);
requeststream.Close();
httpresponse=(HttpWebResponse)httprequest.GetResponse();
if (!(httpresponse.Cookies.Count>0))
{ SetCookie(httpresponse, GetCookie(httprequest)); }
return httpresponse;
}
public static HttpWebResponse GetData(string uri, CookieContainer cookie)
{
HttpWebRequest httprequest;
HttpWebResponse httpresponse;
httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
if (cookie == null)
{ httprequest.CookieContainer = new CookieContainer(); }
else
{ httprequest.CookieContainer = cookie; }
httprequest.Method = "GET";
httprequest.ContentType = "application/x-www-form-urlencoded";
httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
httpresponse = (HttpWebResponse)httprequest.GetResponse();
if (!(httpresponse.Cookies.Count > 0))
{ SetCookie(httpresponse, GetCookie(httprequest)); }
return httpresponse;
}
主:
string uri = "http://s2.kingsera.org";
string userName = "someUserName";
string passWord = "somePassWord";
string postData = "signinUsername=" + userName + "&signinPassword=" + passWord + "&signinRemember=remember";
CookieContainer cookie = new CookieContainer();
HttpWebResponse response = PostData(uri, postData, cookie);
CookieContainer c = GetCookie(response);
看來登錄頁面正在做的事情,使其更加複雜!http://s2.kingsera.org
每一個單一的評論/建議將不勝感激 謝謝提前。
您尚未在[httpWebRequest.Credentials](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials.aspx)屬性中設置任何內容。 – Amicable 2012-07-25 13:46:57
好吧,其實我有。但它看起來並不像我在這個課堂的例子中看到的那些。 – Musketeer 2012-07-25 16:38:42