我遇到了代码问题,因为如果我在Visual Studio中测试它,我会在我的网站上测试它。
我正在尝试在外部网站上进行自动登录。如果我从VS测试它,我在外部网站上正确重定向。从我的网站获得我被重定向到“http://www.MySite.com/cgi-bin/wbc_login / ....”!!!!!
代码如下:
private void MioMetodo(String username, String password)
{
CookieContainer Cookies = new CookieContainer();
Cookie langCookie = new Cookie("pk_lang", "\"italiano\"", "/");
langCookie.Domain = "xxx.yy";
Cookie loginCookie = new Cookie("wc_loginoslimit", "\"" + username + "\"", "/");
loginCookie.Domain = "xxx.yy";
Cookie passwordCookie = new Cookie("wc_passwordoslimit", "\"" + password + "\"", "/");
passwordCookie.Domain = "xxx.yy";
Cookies.Add(langCookie);
Cookies.Add(loginCookie);
Cookies.Add(passwordCookie);
UTF8Encoding encoding = new UTF8Encoding();
String postData = "wc_login=" + username + "&wc_password=" + password + "&limit=0&Avanti=Avanti";
Byte[] data = encoding.GetBytes(postData);
HttpWebRequest myHttpWebRequest = WebRequest.Create("http://xxx.yy/cgi-bin/wbc_login") as HttpWebRequest;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.Referer = "http://xxx.yy/cgi-bin/wbc_login";
myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
myHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4");
myHttpWebRequest.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0");
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.CookieContainer = Cookies;
myHttpWebRequest.ContentLength = data.Length;
Stream newStream = myHttpWebRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myHttpWebResponse = myHttpWebRequest.GetResponse() as HttpWebResponse;
StreamReader streamRead =new StreamReader(myHttpWebResponse.GetResponseStream());
Response.Write(streamRead.ReadToEnd());
streamRead.Close();
myHttpWebResponse.Close();
}
提前完成