【问题标题】:Can I do method 'Get' then 'Post' in a HTTPWebRequest?我可以在 HTTPWebRequest 中执行方法“获取”然后“发布”吗?
【发布时间】:2017-11-13 10:37:31
【问题描述】:

首先,我为我的英语能力道歉^^

目标:我想将 数据 发布到登录网站。 但 data 包含我必须从网站获取的 value(刷新网站后的 value 不同)。 所以,我先执行 'GET' 方法,并获取 ,然后执行 'POST' 方法并得到错误 'This property cannot be set after writing has started.'在“内容长度”中

我的代码:

         HttpWebRequest wr = HttpWebRequest.Create("https://www.fshare.vn/login") as HttpWebRequest;
        wr.KeepAlive = true;

        // get the value
        HttpWebResponse wrep = wr.GetResponse() as HttpWebResponse;
        Stream streamReponse = wrep.GetResponseStream();
        StreamReader reader = new StreamReader(streamReponse);
        string httpDoc = reader.ReadToEnd();
        string fs_csrt = getfs_csrf(httpDoc);

        wr.CookieContainer = new CookieContainer();
        wr.CookieContainer.Add(wrep.Cookies);

        reader.Close();
        streamReponse.Close();
        wrep.Close();


        // Post into website

        string postDataString = @"POST /login fs_csrf={0}
                            &LoginForm%5Bemail%5D=abcd%40yahoo.com.vn
                            &LoginForm%5Bpassword%5D=abcd
                            &LoginForm%5Bcheckloginpopup%5D=0
                            &LoginForm%5BrememberMe%5D=0
                            &yt0=%C4%90%C4%83ng+nh%E1%BA%ADp
                            ";
        postDataString = string.Format(postDataString, fs_csrt);
        MessageBox.Show("I will post: " + postDataString);

        byte[] postDatabyte = Encoding.ASCII.GetBytes(postDataString);



        wr.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";
        wr.Method = "POST";
        wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        wr.KeepAlive = true;
        wr.ContentType = "application/x-www-form-urlencoded";
        //wr.ContentLength = postDatabyte.Length;

        Stream stream = wr.GetRequestStream();
        stream.Write(postDatabyte, 0, postDatabyte.Length);
        stream.Close();

        // get the result
        HttpWebResponse wrep2 = wr.GetResponse() as HttpWebResponse;
        Stream streamReponse2 = wrep2.GetResponseStream();
        StreamReader reader2 = new StreamReader(streamReponse2);
        string httpDoc2 = reader2.ReadToEnd();

        Clipboard.SetText(httpDoc2);
        MessageBox.Show("Post done");   

请帮帮我T_T

【问题讨论】:

  • 为什么要重用wr?
  • 因为,如果重新创建 wr,我需要得到的值将会改变 :(

标签: c# asp.net-web-api


【解决方案1】:

您收到该错误是因为请求类型 Get 不能有正文。

您应该通过调用HttpWebRequestCreate 方法创建另一个请求对象,然后使用该对象发布数据。

HttpWebRequest anotherWR = HttpWebRequest.Create("https://www.fshare.vn/login") as HttpWebRequest;
anotherWR.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";
anotherWR.Method = "POST";
anotherWR.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
anotherWR.KeepAlive = true;
anotherWR.ContentType = "application/x-www-form-urlencoded";
Stream stream = wr.GetRequestStream();
stream.Write(postDatabyte, 0, postDatabyte.Length);
stream.Close();

...
...
...
//and your logic...

【讨论】:

  • 不,如果重新创建请求,我需要获取的 VALUE 将会改变:(
  • 请求对象中没有存储任何内容!你在说什么价值?
  • 每次访问 'fshare.vn',我都会得到一个 'fs_csrt' 值: fs_csrt 用于登录网站.但是当我创建新的请求时 fs_csrt 会改变
  • 您不需要先下载表单html,然后再发布表单。如果您知道在哪里发布表单(表单网址),您可以直接执行此操作。看看here
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 2017-11-21
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多