![](/img/trans.png)
[英]C# post username/password to the login page using windows application
[英]Post Username and Password to login page programmatically
我想使用 asp.net 將用戶名和密碼發布到遠程網站的登錄頁面並傳遞它,以訪問網站上需要登錄的頁面。 換句話說,假設網站上有一個頁面,我想瀏覽它並從中獲取一些東西,但在它之前需要登錄。 如何調用該登錄頁面並從 asp.net 應用程序中發布用戶名和密碼以傳遞它。
提前致謝
將它們作為查詢字符串傳遞的注釋僅適用於GET
參數。 這適用於POST
。 如您所見,我使用表單條目username
、 password
和login
(按鈕),但在您的情況下,它可能是完全不同的值。 所以使用像Fiddler這樣的工具來捕捉正常登錄正在發送的值。 當我做類似的事情時,我不得不使用一個 cookie 感知網絡客戶端,因為該服務使用 cookies 來獲取 session 值。 您嘗試訪問的服務可以使用查詢會話字符串 - 它完全取決於服務。
我在執行此操作時遇到的另一個問題是,我必須從 html 頁面獲取 session id 並將其發送以進行記錄。 我沒有在此處包含該代碼,但如果您需要它,我有它:)
var client = new CookieAwareWebClient();
client.Encoding = Encoding.UTF8;
// Post values
var values = new NameValueCollection();
values.Add("username", someusername);
values.Add("password", somepassword);
values.Add("login", "Login"); //The button
// Logging in
client.UploadValues(loginPageUrl, values); // You may verify the result. It works with https :)
// Download some secret page
var html= client.DownloadString(someurl);
CookieAwareWebClient
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookieContainer;
}
return request;
}
}
嘗試閱讀此-> http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/
如果您使用 firefox - 下載“tamper-data”和“live http-headers”以查看將哪些信息傳遞到登錄站點
使用實時 http-headers,您可以查看 POST 中傳遞了哪些數據
假設您想重定向到 www.ABC.com
構造查詢字符串,如
http://abc.com/products.aspx?field1=value1&field2=value2
並在另一頁上獲取此值。 但是推薦使用POST方式,希望對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.