登录网站异常

本文关键字:异常 网站 登录 | 更新日期: 2023-09-27 18:28:16

嗨,我是c#网络抓取的新手。

我想登录到http://telelisting.ca/但我不能。我使用了WebClientWebBrowser和现在的HttpWebRequest,但一直发生相同的错误,即服务器异常500。

有人能帮我做错事吗??

CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://telelisting.ca/");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "http://telelisting.ca/";
string postData = String.Format("ctl00_LoginPopup1_Login1_UserName={0}&ctl00_LoginPopup1_Login1_Password={1}&__LASTFOCUS={2}&__EVENTTARGET={3}&__EVENTARGUMENT={4}&__VIEWSTATE={5}&__EVENTVALIDATION={6}&ctl00_LoginPopup1_Login1_LoginButton={7}&ctl00_LoginPopup1_hiddenImagePath={8}&ctl00_cphContent_hiddenVideoDialogTitle={9}&ctl00_cphContent_hiddenShowDefaultVideo={10}", "fg@xyz.com", "abcede", "", "", "", "/wEPDwUKMTYzMzk2MDQwNQ9kFgJmD2QWAgIDDxYEHgVjbGFzc2UeCG9udW5sb2FkBQ9PbkVuZFNlc3Npb24oKTsWAgIBD2QWDAIHD2QWBAIDD2QWAgIDDw8WBB4EVGV4dAUVRm9yZ290IHlvdXIgcGFzc3dvcmQ/HgtOYXZpZ2F0ZVVybAUWfi9wYXNzd29yZHJlY292ZXIuYXNweGRkAgUPDxYCHg9WYWxpZGF0aW9uR3JvdXAFGGN0bDAwJExvZ2luUG9wdXAxJExvZ2luMWRkAgkPZBYCAgEPFgIeBGhyZWYFHmphdmFzY3JpcHQ6U2hvd1ZpZGVvQ29udGVudCgpO2QCCw9kFgICAQ9kFgYCCw8PFgIfAwURL0RlZmF1bHQuYXNweD9sPTBkZAIND2QWAgIBD2QWAmYPFgIfAAURRW1wdHlTaG9wcGluZ0NhcnQWAgIBDw8WAh8CB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pNLiBTbWl0aC5kZAIRDw9kDxAWAWYWARYCHwkFBWVuLVVTFgECBmRkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBSNjdGwwMCRMb2dpblBvcHVwMSRMb2dpbjEkUmVtZW1iZXJNZQUpY3RsMDAkTG9naW5Qb3B1cDEkTG9naW4xJExvZ2luSW1hZ2VCdXR0b25RUp74UUPL72rYSQL3cfYMy9DWZU4l5e0kF3vCJf5fkQ==", "/wEWCQL9vYfeCAKVyobIBgKB94O0CwKz7JHYDQLsu7CWDgL31sOaAQLmjZ/bDQLto+ilAwLC4KCXAqOJh1kiYkswzxstW/o4u1wgJbA0/EnFO2L8sDvV8fVl", "Log In", "/HSIControl", "Telelisting Instruction Video", "");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
//Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
getRequest.ContentType = "text/html";
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
   string sourceCode = sr.ReadToEnd();
   Debug.WriteLine(sourceCode);
} 

登录网站异常

500范围内的服务器错误意味着服务器端出现问题。从你的请求来看,我认为它可能与你发布的Viewstate变量有关。服务器将尝试根据您提交的状态重新构建页面。这不是你只是复制粘贴的东西。您是否尝试过只发送与登录本身相关的参数(名称、密码、单击按钮的名称)?

对于您的工作,请参阅fiddler运行相同的请求,并查看cookie匹配和其他发布参数。

也可以尝试:

postparameter=  UrlEncode(postparameter) 

在将它们转换为字节之前,看看这是否有效。

也许是这样。

为什么不转到您想在浏览器中抓取的确切页面,然后导出完整的COOKIE集以手动附加到脚本的标题?