httpwebrequest、httpwebresponse 不太工作

发布于 2024-12-20 08:26:59 字数 1523 浏览 1 评论 0原文

我有一个 Windows 窗体应用程序,用于登录网站并检索网页的内容(html 代码)。

我从 Windows 窗体应用程序更改了代码,使其能够与 .aspx 页面一起使用,但我遇到了问题。下面的字符串 (thepage) 中没有保存任何内容:

private void button1_Click(object sender, EventArgs e)
        {
            string postData = "bla bla bla...";
            CookieContainer tempCookies = new CookieContainer();
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteData = encoding.GetBytes(postData);

        HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://bla bla bla...php");
        postReq.Method = "POST";
        postReq.KeepAlive = true;
        postReq.CookieContainer = tempCookies;
        postReq.ContentType = "application/x-www-form-urlencoded";
        postReq.Referer = "http://bla bla bla...php";
        postReq.UserAgent = "Opera/9.80 (Windows NT 6.1; U; en-GB) Presto/2.9.168 Version/11.52";
        postReq.ContentLength = byteData.Length;

        Stream postreqstream = postReq.GetRequestStream();
        postreqstream.Write(byteData, 0, byteData.Length);
        postreqstream.Close();
        HttpWebResponse postresponse = default(HttpWebResponse);

        postresponse = (HttpWebResponse)postReq.GetResponse();
        tempCookies.Add(postresponse.Cookies);
        logincookie = tempCookies;
        StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());

        string thepage = postreqreader.ReadToEnd();
        TextBox1.Text = thepage;

表单应用程序工作正常,但 .aspx 却不行。不知道为什么。请帮我解决这个问题。谢谢。

I have a windows form application which serves to login to a website and retrieve the content (html code) of a webpage.

I change my code from windows form application to make it work with an .aspx page, but I have problems. Nothing gets saved in the string (thepage) below:

private void button1_Click(object sender, EventArgs e)
        {
            string postData = "bla bla bla...";
            CookieContainer tempCookies = new CookieContainer();
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteData = encoding.GetBytes(postData);

        HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://bla bla bla...php");
        postReq.Method = "POST";
        postReq.KeepAlive = true;
        postReq.CookieContainer = tempCookies;
        postReq.ContentType = "application/x-www-form-urlencoded";
        postReq.Referer = "http://bla bla bla...php";
        postReq.UserAgent = "Opera/9.80 (Windows NT 6.1; U; en-GB) Presto/2.9.168 Version/11.52";
        postReq.ContentLength = byteData.Length;

        Stream postreqstream = postReq.GetRequestStream();
        postreqstream.Write(byteData, 0, byteData.Length);
        postreqstream.Close();
        HttpWebResponse postresponse = default(HttpWebResponse);

        postresponse = (HttpWebResponse)postReq.GetResponse();
        tempCookies.Add(postresponse.Cookies);
        logincookie = tempCookies;
        StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());

        string thepage = postreqreader.ReadToEnd();
        TextBox1.Text = thepage;

The form application works fine, but the .aspx does not. Have no idea why. Please, help me with that. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-12-27 08:26:59

在这里,在黑暗中拍摄,但尝试

postReq.Credentials = CredentialCache.DefaultCredentials;

如果失败,您可以随时打开 Fiddler 或 FireBug 来查看来回发送的请求和响应。也许您会发现随请求一起发送的数据不是您所期望的?

Shot in the dark, here, but try

postReq.Credentials = CredentialCache.DefaultCredentials;

Failing that, you could always open up Fiddler or FireBug to see the request and response being sent back and forth. Maybe you'll see that the data you are sending along with the request isn't what you expect it to be?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文