使用webclient保持会话活动

时间:2015-03-13 14:39:20

标签: webclient

我试图对连接到网站的应用程序进行编程而不是垃圾邮件发送者,但有些事情是这样的。

但有一个问题:

在该网站中,您将通过html表单登录,我知道目标php页面和参数,所以使用c#我这样登录:

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-urlencoded";
    wc.UploadString("http://example.com/login/check.php", "username=US&password=PW");
    MessageBox.Show(result);
}

登录后,当我想(例如)发送帖子(如下面的代码)时,答案是你没有进入!

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-urlencoded";
    wc.UploadString("http://example.com/post/send.php", "title=T&body=B");
    MessageBox.Show(result);
}

我认为问题在于会话已关闭。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

我不知道你是否找到答案但是试试这个

    WebClient webClient = new WebClient();
    string url = @"http://example.com/login/check.php";
    var postData = new System.Collections.Specialized.NameValueCollection();
byte[] bytes = webClient.UploadValues(url,"POST",postData);

string text = System.Text.Encoding.ASCII.GetString(bytes);

使用Fiddler查找参数。