I want to extract data from HTML of a website that needs to be logged in. I am searhing the web for 4 days with no clear results.
I need an answer for Asp.net/C#.
The information I collected so far:
Login Page Source Code:
<form method="post" action="/" id="login_form" onsubmit="return ajax_submit('#login_form',post_login)">
<table class="login_table" cellspacing="0" cellpadding="0">
<tr>
<td><label for="email">Email</label></td>
<td><label for="password">Password</label></td>
<td></td>
</tr>
<tr>
<td>
<div class='sign_up_error' id="sign_up_error" style='margin-top:0;margin-left:-207px;'></div>
<div class='light_input' style='width:150px'>
<input type="text" name="email" id="email" class="login_field" style='width:142px' tabindex="1" />
</div>
</td>
<td>
<div class='light_input' style='width:150px'>
<input type="password" name="password" id="password" class="login_field" style='width:142px' tabindex="2" />
</div>
</td>
<td>
<span class='button' id="login_button" onclick="ajax_submit('#login_form',post_login);" tabindex="3"><span class='button_border'>Log In <img src='/pics/cf_mini_arrow_right.png'></span></span>
<input type="submit" class="" style="width:0px; height: 0px; overflow:hidden; border: none;" name="submit_login"/>
</td>
</tr>
<tr>
<td><input type="checkbox" name="remember" id="remember" value="1" tabindex="4"/><label for="remember">Remember me</label></td>
<td class='right' style='padding-right:5px;'><a class='weight_normal' href="/forgot-password/">Forgot password?</a></td>
<input type="hidden" name="login_now" value="yes" />
<td><input type='hidden' name="login_submit" id="login_submit" /></td>
</tr>
</table>
</form>
Request Header is like (wsName: WebSite Name):
Cookie of Request Header
wsName [css]=medium_text;
cc_cookie_accept=cc_cookie_accept;
My Code (which doesnt work and taken from here ) is like:
string loginURL = "http://www.*wsName*.com/ajax/login.ajax.php";
string loginURL2 = "http://www.*wsName*.com";
string formDataStr = "email=???&password=???&login_now=yes&login_submit=";
//First request: Get the cookies
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginURL);
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;
//Second request: POST the form data and recover the cookies from the first request..
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(loginURL);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
//getRequest.Referer = "http://www.*wsName*.com/index.php";
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(formDataStr);
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();
}
I used both links (loginURL, loginURL2), modified and tried many things but at the end i have a very big zero. This is the first time i needed to work with HttpWebRequest and HttpWebResponse. So i think i miss something here.
Please help, thanks in advance.
您可以使用http请求配置文件工具,如fiddler记录您的Web浏览器的请求详细信息,并再次运行您的c#代码,比较两个结果的差异。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.