POST information simulates login to obtain the page content, post information acquisition page

Source: Internet
Author: User

POST information simulates login to obtain the page content, post information acquisition page

One of the most recent projects is to simulate the requirement of accessing a fixed page to obtain content after logon. jq ajax does not seem to support cross-origin requests at the beginning. And then use the HttpWebRequest object in. net. At the beginning, access to the second page is always unavailable. It seems that the login verification is not passed, and the submission is normal using postman simulation. Then, it is found that the cookies were not associated with the second request, after Association, the request is normal.

            string wurl="";                       string username="haxinet";            string userpwd="haxinet";            CookieContainer cookies = new CookieContainer();            Encoding encode = System.Text.Encoding.UTF8;            byte[] arrB = encode.GetBytes("user_name=" + username + "&user_passwd=" + userpwd + "&keepuser=1");                    System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(wurl);            myReq.Method = "POST";            myReq.ContentType = "application/x-www-form-urlencoded";            myReq.ContentLength = arrB.Length;            myReq.CookieContainer = cookies;            Stream outStream = myReq.GetRequestStream();            outStream.Write(arrB, 0, arrB.Length);            outStream.Close();            System.Net.WebResponse myResp = null;            try            {                myResp = myReq.GetResponse();            }            catch (Exception e)            {                          }            Stream ReceiveStream = myResp.GetResponseStream();            StreamReader readStream = new StreamReader(ReceiveStream, encode);            Char[] read = new Char[256];            int count = readStream.Read(read, 0, 256);            string str = null;            while (count > 0)            {                str += new String(read, 0, count);                count = readStream.Read(read, 0, 256);            }            readStream.Close();            myResp.Close();            string wuurl = "";            HttpWebRequest myReq2 = (HttpWebRequest)HttpWebRequest.Create(wuurl);            myReq2.Method = "GET";            myReq2.CookieContainer = cookies;            using (WebResponse wr = myReq2.GetResponse())            {                StreamReader reader = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);                string result = reader.ReadToEnd();                retStr = result;            }    

  


How to use java to simulate the post method and log on to douban.com to obtain personal information

Why not use HttpClient?
Teach you a way to use FireBug to view the request when you log on
Then use Socket to simulate the request process.
 
Php simulates post submission to obtain data.

Score ~

Use CURL

$ Url = 'HTTP: // url/json. php ';
$ CurlPost = "a = 123 & B = 456 ";
$ Ch = curl_init (); // initialize curl
Curl_setopt ($ ch, CURLOPT_URL, $ url); // submit to the specified webpage
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // set the header
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is a string and output to the screen.
Curl_setopt ($ ch, CURLOPT_POST, 1); // post submission method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
$ Data = curl_exec ($ ch); // run curl
Curl_close ($ ch );
Print_r ($ data); // output result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.