Request. method to be setUppercase post,Otherwise, error 403 is returned.
The post method may not be able to obtain post data on the server, so you must set contenttype to application/X-WWW-form-urlencoded.
The following is an example. If you do not need to capture exceptions, you can add them yourself ~~~
Webrequest Req = webrequest. create ("http://expert.csdn.net/Expert/topic/1581/1581934.xml"); <br/> req. method = "Post"; <br/> req. contenttype = "application/X-WWW-form-urlencoded"; <br/> byte [] postdata = system. text. encoding. getencoding ("gb2312 "). getbytes ("temp = 1063959 & userid = 654321"); // Post Data <br/> req. contentlength = postdata. length; <br/> stream poststream = req. getrequeststream (); <br/> poststream. write (postdata, 0, postdata. length); <br/> poststream. close (); <br/> webresponse res = req. getresponse (); <br/> system. text. encoding resencoding = system. text. encoding. getencoding ("gb2312"); // The received encoding <br/> streamreader reader = new streamreader (res. getresponsestream (), resencoding); <br/> string html = reader. readtoend (); // html received <br/> reader. close (); <br/> res. close ();
Two key lines of code:
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";