简体   繁体   English

httpwebrequest,httpwebresponse不太起作用

[英]httpwebrequest, httpwebresponse not quite working

I have a windows form application which serves to login to a website and retrieve the content (html code) of a webpage. 我有一个Windows窗体应用程序,用于登录网站并检索网页的内容(html代码)。

I change my code from windows form application to make it work with an .aspx page, but I have problems. 我从Windows窗体应用程序更改了我的代码,使其可以与.aspx页一起使用,但是我遇到了问题。 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. 表单应用程序可以正常工作,但.aspx不能。 Have no idea why. 不知道为什么。 Please, help me with that. 请帮我。 Thanks. 谢谢。

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. 失败的话,您总是可以打开Fiddler或FireBug来查看请求和响应的来回发送。 Maybe you'll see that the data you are sending along with the request isn't what you expect it to be? 也许您会看到与请求一起发送的数据不是您期望的那样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM