请求已中止:连接意外关闭

时间:2011-09-03 16:36:50

标签: c# httpwebrequest webrequest

我希望我的程序能够做两个背靠背的webrequests。第一个填写页面上的表单。第二个Web请求提交来自先前webrequest的数据的页面。

我试过这样做,但我一直收到这个错误:

  

请求已中止:连接意外关闭

继承我的代码:

public void signup(string name, string email, string password, string username)
{

    try
    {
        System.Net.ServicePointManager.Expect100Continue = false;

        string postData = "user[name]=" + name + "&user[email]=" + email + "&user[user_password]=" + password + "&user[screen_name]=" + username + "";

        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byteData = encoding.GetBytes(postData);

        HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("https://twitter.com/signup");
        postReq.Method = "POST";
        postReq.KeepAlive = true;
        postReq.ContentType = "application/x-www-form-urlencoded";
        postReq.Referer = "Referer: https://twitter.com/signup";
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
        postReq.ContentLength = byteData.Length;

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

        postresponse = (HttpWebResponse)postReq.GetResponse();
        StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());
        StreamReader reader = new StreamReader(postresponse.GetResponseStream());

        string theusercp = reader.ReadToEnd();

        if (theusercp.Contains("Keep me logged-in on this computer"))
        {

            string postdata = "authenticity_token=&user%5Bname%5D=" + txtBoxImportNames1.Text + "" + txtBoxImportNames2.Text + "&user%5Bemail%5D=" + listBox4.SelectedItem.ToString() + "&user%5Buser_password%5D=" + txtBoxPassword.Text + "&user%5Bscreen_name%5D=" + txtBoxUsernames.Text + "&user%5Bremember_me_on_signup%5D=&context=&user%5Bdiscoverable_by_email%5D=1&user%5Bsend_email_newsletter%5D=1";

            UTF8Encoding encoding1 = new UTF8Encoding();
            byte[] bytedata = encoding.GetBytes(postdata);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://twitter.com/account/create");
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = "Referer: https://twitter.com/signup";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
            request.ContentLength = byteData.Length;

            Stream postreqstream2 = request.GetRequestStream();
            postreqstream.Write(byteData, 0, byteData.Length);
            postreqstream2.Close();
            HttpWebResponse response = default(HttpWebResponse);

            postresponse = (HttpWebResponse)request.GetResponse();
            StreamReader stream2 = new StreamReader(response.GetResponseStream());
            StreamReader reader2 = new StreamReader(response.GetResponseStream());

            string twitter = reader2.ReadToEnd();


            richTextBox1.Text = twitter;

            webBrowser1.DocumentText = twitter;
        }
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

0 个答案:

没有答案