简体   繁体   English

我可以在HTTPWebRequest中执行方法“获取”然后“发布”吗?

[英]Can I do method 'Get' then 'Post' in a HTTPWebRequest?

First, I apologize for the english ability ^^ 首先,我对英语能力表示歉意。

target: I want POST a data to a Login website. 目标:我想将数据发布到登录网站。 but the data contain a value (the value different after refresh website) that i must GET from website. 但是数据包含一个我必须从网站获取的 (刷新网站后该不同)。 So, i do 'GET' method first, and get the value , then i do 'POST' method & i get the error 'This property cannot be set after writing has started.' 因此,我先执行'GET'方法,然后获取 ,然后执行'POST'方法,并收到错误消息'开始写入后无法设置此属性'。 in 'content-length' 在“内容长度”中

My code: 我的代码:

         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");   

Please help me T_T 请帮我T_T

You are getting that error because request Type Get can not have body. 您正在收到该错误,因为请求类型Get不能具有主体。

You should create another request object by invoking Create method of HttpWebRequest and then use that object to post the data. 您应该通过调用HttpWebRequest Create方法来创建另一个请求对象,然后使用该对象发布数据。

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...

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

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