急!C#利用HttpWebRequest怎么获取远程html代码 20
本地准备了一个form表单,然后通过页面最下方的一个简单js的form提交到远程一个接收链接,希望能够通过HttpWebRequest自动获取远程处理结果页面,但试了以下...
本地准备了一个form表单,然后通过页面最下方的一个简单js的form提交到远程一个接收链接,希望能够通过HttpWebRequest自动获取远程处理结果页面,但试了以下方法不行:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.Timeout = 300;
request.MaximumAutomaticRedirections = 2;
request.AllowAutoRedirect = true;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
请高手赐教!
已解决:
protected void Page_Load(object sender,EventArgs e) {
Uri uri = new Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312");
string data = "field-keywords=ASP.NET 2.0";
if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
Response.Write(tmp);
}
} 展开
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.Timeout = 300;
request.MaximumAutomaticRedirections = 2;
request.AllowAutoRedirect = true;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
请高手赐教!
已解决:
protected void Page_Load(object sender,EventArgs e) {
Uri uri = new Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312");
string data = "field-keywords=ASP.NET 2.0";
if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
Response.Write(tmp);
}
} 展开
1个回答
展开全部
以前写的一个方法:
//根据Url地址得到网页完整的HTML源码
public string GetWebContent(string Url, string Encod)
{
string srcString = "";
Uri Urled = new Uri(Url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Urled);
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
if (response.ContentEncoding.ToLower() == "gzip")
{//针对IIS压缩的页面
streamReceive = new GZipStream(streamReceive, CompressionMode.Decompress);
}
Encoding encoding = Encoding.GetEncoding(Encod);
StreamReader streamReader = new StreamReader(streamReceive, encoding);
srcString = streamReader.ReadToEnd();
return srcString;
}
----
补充:
那你先抓取js文件。然后正则分析出那个地址再抓啊。。。。反正首先你需要知道一个url,然后层层的剥。。。。
//根据Url地址得到网页完整的HTML源码
public string GetWebContent(string Url, string Encod)
{
string srcString = "";
Uri Urled = new Uri(Url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Urled);
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
if (response.ContentEncoding.ToLower() == "gzip")
{//针对IIS压缩的页面
streamReceive = new GZipStream(streamReceive, CompressionMode.Decompress);
}
Encoding encoding = Encoding.GetEncoding(Encod);
StreamReader streamReader = new StreamReader(streamReceive, encoding);
srcString = streamReader.ReadToEnd();
return srcString;
}
----
补充:
那你先抓取js文件。然后正则分析出那个地址再抓啊。。。。反正首先你需要知道一个url,然后层层的剥。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询