前些天有个要求要从某个网站上取信息,但是该网站要求用post方式访问的.现在这里贴两个google到的方法. using System.Net; static string WebClinetPost(string url, string postData,string encodeType,out string err) err=string.Empty; 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 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; err = string.Empty; } catch(Exception ex) err = ex.Message; } 调用代码: 另外,再附上javascript实现的代码吧: var postData = “proj_id=600197&theSubmit=600197”; function GetXMLHTTPRequest() http://www.cnblogs.com/lwyaster/archive/2007/01/09/615815.html
{
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”,”http://xxxxx/xxxxxxxxxxxxxxxxxxxxxxxx“);
//myWebClient.Headers.Add(“Accept-Language”,”zh-cn”);
myWebHeaderCollection = myWebClient.Headers;
byteArray = encoding.GetBytes(postData);
responseArray = myWebClient.UploadData(uriString,”POST”,byteArray);
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)
{
instream = response.GetResponseStream();
sr = new StreamReader( instream, encoding );
string content = sr.ReadToEnd();
return content;
{
return string.Empty;
}
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);
<script language=javascript>
function GetData()
{
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;
}
{
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>
用post方法从网上抓取信息_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用post方法从网上抓取信息_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧