试图在C#中发送使用帖子的用户名/密码 [英] Trying to send username/password useing post in C#
问题描述
我正在尝试使用WebRequest或WebClient登录网站,但似乎不太顺利。
其中一个原因可能是因为我不太了解它的逻辑。
无论如何这里是我目前使用的代码任何帮助都非常感谢。
< pre lang =c#> private void button2_Click( object sender,EventArgs e)
{
string URLAuth = http://www.lyndatobin-howes.com/wp-login.php;
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData [ log] = xxxx;
formData [ pwd] = xxxx;
formData [ wp-submit] = 登录;
formData [ redirect_to] = http://www.lyndatobin-howes.com/wp-login.php;
formData [ testcookie] = 1;
byte [] responseBytes = webClient.UploadValues(URLAuth, POST,formData);
string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose();
// string searchammount = comboBox1.SelectedItem.ToString();
WebRequest Logingrequest = WebRequest.Create( http://www.lyndatobin-howes.com/wp-login .PHP跨度>);
Logingrequest.Credentials = CredentialCache.DefaultCredentials;
Logingrequest.ContentType = application / x-www-form-urlencoded;
Logingrequest.Method = POST;
WebResponse response = Logingrequest.GetResponse();
流dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
richTextBox2.Text = responseFromServer;
reader.Close();
dataStream.Close();
response.Close();
} http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx [ ^ ]
或尝试这种方式..
public static void PostString( string 地址)
{
string data = 时间=上午12:00温度= 50;
string method = POST跨度>;
WebClient client = new WebClient();
string reply = client.UploadString(address,method,data);
Console.WriteLine(回复);
}
或试用此链接。
http://msdn.microsoft.com/en-us/library/debx8sh9 .aspx [ ^ ]
我尝试了一种不同的方式:
任何帮助都会非常感激。
WebRequest Logingrequest = WebRequest.Create( http://www.lyndatobin- howes.com/wp-login.php跨度>);
Logingrequest.Credentials = CredentialCache.DefaultCredentials;
Logingrequest.ContentType = application / x-www-form-urlencoded;
Logingrequest.Method = POST;
string postData = log = admin& pwd = m7625zsw& wp-submit =登录& redirect_to = http://www.lyndatobin-howes.com/wp-login.php& testcookie = 1;
byte [] byteArray = Encoding.UTF8.GetBytes(postData);
// 设置WebRequest的ContentLength属性。
Logingrequest.ContentLength = byteArray.Length;
流dataStream = Logingrequest.GetRequestStream();
dataStream.Write(byteArray, 0 ,byteArray.Length);
WebResponse response = Logingrequest.GetResponse();
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
richTextBox2.Text = responseFromServer;
reader.Close();
dataStream.Close();
response.Close();
I am trying to "log in a website using WebRequest or WebClient, and doesn't seem to go very well.
One of the reasons is probably because i don't understand the logic of it well enough.
Anyway here is the code i currently use Any help is highly appreciated.
private void button2_Click(object sender, EventArgs e)
{
string URLAuth = "http://www.lyndatobin-howes.com/wp-login.php";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["log"] = "xxxx";
formData["pwd"] = "xxxx";
formData["wp-submit"] = "Log In";
formData["redirect_to"] = "http://www.lyndatobin-howes.com/wp-login.php";
formData["testcookie"] = "1";
byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose();
//string searchammount = comboBox1.SelectedItem.ToString();
WebRequest Logingrequest = WebRequest.Create("http://www.lyndatobin-howes.com/wp-login.php");
Logingrequest.Credentials = CredentialCache.DefaultCredentials;
Logingrequest.ContentType = "application/x-www-form-urlencoded";
Logingrequest.Method = "POST";
WebResponse response = Logingrequest.GetResponse();
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
richTextBox2.Text = responseFromServer;
reader.Close();
dataStream.Close();
response.Close();
}
http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx[^]
or try this way..
public static void PostString (string address) { string data = "Time = 12:00am temperature = 50"; string method = "POST"; WebClient client = new WebClient (); string reply = client.UploadString (address, method, data); Console.WriteLine (reply); }
or try this link.
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx[^]
I've tried a different way still nothing:
any help will be extremely appreciated.
WebRequest Logingrequest = WebRequest.Create("http://www.lyndatobin-howes.com/wp-login.php"); Logingrequest.Credentials = CredentialCache.DefaultCredentials; Logingrequest.ContentType = "application/x-www-form-urlencoded"; Logingrequest.Method = "POST"; string postData = "log=admin&pwd=m7625zsw&wp-submit=Log In&redirect_to=http://www.lyndatobin-howes.com/wp-login.php&testcookie=1"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentLength property of the WebRequest. Logingrequest.ContentLength = byteArray.Length; Stream dataStream = Logingrequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); WebResponse response = Logingrequest.GetResponse(); dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); richTextBox2.Text = responseFromServer; reader.Close(); dataStream.Close(); response.Close();
这篇关于试图在C#中发送使用帖子的用户名/密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!