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