Vor ein paar Tagen gab es eine Anfrage zum Abrufen von Informationen von einer bestimmten Website, aber die Website erfordert einen Post-Zugriff. Hier sind zwei Methoden, die bei Google gefunden wurden:
System.Net
(String URL, String PostData, String EncodeType)
.,out string fehler)
{
string uriString = url;
byte[] byteArray;
byte[] ResponseArray;
//postData = "checkvalue=32&bbb=%CD%B6%C6%B1&ilc=0&kkk=22";
Kodierungkodierung = Encoding.GetEncoding(encodeType);
versuchen
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
//myWebClient.Headers.Add("Referer"," http://xxxxx/xxxxxxxxxxxxxxxxxxxxxxxx ");
//myWebClient.Headers.Add("Accept-Language","zh-cn");
myWebHeaderCollection = myWebClient.Headers;
byteArray =kodierung.GetBytes(postData);
ResponseArray = myWebClient.UploadData(uriString,"POST",byteArray
err=string.Empty
);
Codierung zurückgeben.GetString(responseArray);
}
Catch (Ausnahme ex)
{
err=ex.Message;
return string.Empty;
}
}
öffentlicher statischer String HttpWebResponsePost(string url, string postData,string encodeType,out string err)
{
Stream outstream = null;
StreamReader
sr
= null;
HttpWebRequest
request
=
null
;
request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = cookieContainer
;
request.Method
= "POST"
;
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,kodierung );
string content = sr.ReadToEnd();
err = string.Empty;
return content;
}
Catch(Ausnahme ex)
{
err = ex.Message;
return string.Empty
}
}
Anrufcode:
string url=" http://xxxxxx/xxxxxxxx/scrrm00542.jsp ";
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);
Hängen Sie außerdem den Javascript-Implementierungscode an:
Funktion 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;
}
Funktion 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];
versuchen
{
var x = neues ActiveXObject(progID);
x zurückgeben;
}
fangen (e)
{}
}
}
http://www.cnblogs.com/lwyaster/archive/2007/01/09/615815.html