The previous day, I requested to retrieve information from a website, but the website requested post access. Now I am posting two Google methods here.
Using system. net;
Static string webclinetpost (string URL, string postdata, string encodetype, out string ERR)
{
String uristring = URL;
Byte [] bytearray;
Byte [] responsearray;
// Postdata = "checkvalue = 32 & BBB = % Cd % B6 % C6 % B1 & ILC = 0 & KKK = 22 ";
Encoding encoding = encoding. getencoding (encodetype );
Try
{
WebClient mywebclient = new WebClient ();
Webheadercollection mywebheadercollection;
Mywebclient. headers. Add ("Content-Type", "application/X-WWW-form-urlencoded ");
// Mywebclient. headers. Add ("Referer ","");
// Mywebclient. headers. Add ("Accept-language", "ZH-CN ");
Mywebheadercollection = mywebclient. headers;
Bytearray = encoding. getbytes (postdata );
Responsearray = mywebclient. uploaddata (uristring, "Post", bytearray );
Err = string. empty;
Return encoding. getstring (responsearray );
}
Catch (exception ex)
{
Err = ex. message;
Return string. empty;
}
}
Public static string httpwebresponsepost (string URL, string postdata, string encodetype, out string ERR)
{
Stream outstream = NULL;
Stream instream = NULL;
Streamreader sr = NULL;
Httpwebresponse response = NULL;
Httpwebrequest request = NULL;
Encoding encoding = encoding. getencoding (encodetype );
Byte [] DATA = encoding. getbytes (postdata );
Try
{
Request = webrequest. Create (URL) as httpwebrequest;
Cookiecontainer = new cookiecontainer ();
Request. cookiecontainer = cookiecontainer;
Request. allowautoredirect = true;
Request. method = "Post ";
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. contentlength = data. length;
Outstream = request. getrequeststream ();
Outstream. Write (data, 0, Data. Length );
Outstream. Close ();
Response = request. getresponse () as httpwebresponse;
Instream = response. getresponsestream ();
Sr = new streamreader (instream, encoding );
String content = Sr. readtoend ();
Err = string. empty;
Return content;
}
Catch (exception ex)
{
Err = ex. message;
Return string. empty;
}
}
Call code:
String url = "";
String postdata = "proj_id = 600197 & thesubmit = 600197 ";
String encodetype = "UTF-8 ";
String err = "";
// String content = httpwebresponsepost (URL, postdata, encodetype, out ERR );
String content = webclinetpost (URL, postdata, encodetype, out ERR );
In addition, add the Javascript implementation code:
<Script language = JavaScript>
Function getdata ()
{
VaR postdata = "proj_id = 600197 & thesubmit = 600197 ";
VaR HTTP = new getxmlhttprequest ();
HTTP. Open ('post', 'HTTP: // xxxxxxxxxx/xxxxxx/scrrm00542.jsp ', false );
HTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
HTTP. Send (postdata );
Return HTTP. responsetext;
}
Function getxmlhttprequest ()
{
VaR progids = ["msxml2.xmlhttp. 5.0", "msxml2.xmlhttp. 4.0", "msxml2.xmlhttp. 3.0", "msxml2.xmlhttp", "Microsoft. XMLHTTP"];
For (VAR I = 0; I <progids. length; ++ I)
{
VaR progid = progids [I];
Try
{
VaR x = new activexobject (progid );
Return X;
}
Catch (E)
{}
}
}
</SCRIPT>