C# 在Asp.net中登录网站程序

C# 在Asp.net中登录网站程序,c#,php,asp.net,ajax,cookies,C#,Php,Asp.net,Ajax,Cookies,我想从需要登录的网站的HTML中提取数据。我在网上搜索了4天,没有明显的结果 我需要Asp.net/C的答案 到目前为止我收集的信息: <form method="post" action="/" id="login_form" onsubmit="return ajax_submit('#login_form',post_login)"> <table class="login_table" cellspacing="0" cellpadding="0">

我想从需要登录的网站的HTML中提取数据。我在网上搜索了4天,没有明显的结果

我需要Asp.net/C的答案

到目前为止我收集的信息:

<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>
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();
} 
  • 我需要使用HttpWebRequest和HttpWebResponse
  • 我得和饼干一起工作
  • 该网站正在使用PHP、JS/AJAX
  • postData类似于“email=???&password=??&login\u now=yes&login\u submit=”
  • 登录按钮重定向到“.wsName.com/ajax/Login.ajax.php”
登录页面源代码:

<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>
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();
} 
我使用了两个链接(loginURL,loginURL2),修改并尝试了很多东西,但最后我得到了一个非常大的零。这是我第一次需要使用HttpWebRequest和HttpWebResponse。所以我想我错过了一些东西


请帮助,提前感谢。

您可以使用http请求配置文件工具,如fiddler记录您的web浏览器请求详细信息,并再次运行c#代码,比较两个结果的差异。

您可以使用http请求配置文件工具,如fiddler记录您的web浏览器请求详细信息,然后再次运行你的c代码,比较两个结果的不同之处。

看看这个答案:谢谢@Eriknordinwarlberg。但是在我的代码中,我不是已经这样做了吗(在你发布的链接中写了什么)?第一步我得到饼干。在第二个我使用它们。在另一段代码中,我试图操纵上面“请求头信息”附带的cookies,但它也不起作用。但是在我的代码中,我不是已经这样做了吗(在你发布的链接中写了什么)?第一步我得到饼干。在第二个我使用它们。在另一段代码中,我试图操纵上面“请求头信息”附带的cookie,但它也不起作用。