如何获取ASPX网页上的信息
推荐于2016-08-22
展开全部
不可以用JS做,那肯定跟XMLHTTP没关系了,只能用HttpWebRequest 了
如
public static CookieContainer Cook = new CookieContainer();
/// <summary>
/// 发送有cookie的http请求 code by Depluin
/// </summary>
/// <param name="URL">要读取的网址</param>
/// <param name="PostData">要发送的postdata</param>
/// <returns>返回的字符串</returns>
public static string CreatRequestGetResponse(string URL, string PostData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.CookieContainer = Cook;
myRequest.KeepAlive = true;
myRequest.Method = "POST";
string postdata = PostData;
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
byte[] postarr = encode.GetBytes(postdata);
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postarr.Length;
Stream outStream = myRequest.GetRequestStream();
outStream.Write(postarr, 0, postarr.Length);
outStream.Close();
WebResponse myResponse = myRequest.GetResponse();
StreamReader SReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
return SReader.ReadToEnd();
}
用的时候用CreatRequestGetResponse("目标网址","要传送的postdata")
比如<%=CreatRequestGetResponse("www.sina.com.cn","") %>
那你直接把这段方法作为runat =server 的脚本扔到aspx里面不就行了?
抱歉,我只授人以渔,不授人以鱼。关键方法都放出来了,如果你连runat=server脚本怎么用都不会,我劝你还是重新学学asp.net再写程序......
如
public static CookieContainer Cook = new CookieContainer();
/// <summary>
/// 发送有cookie的http请求 code by Depluin
/// </summary>
/// <param name="URL">要读取的网址</param>
/// <param name="PostData">要发送的postdata</param>
/// <returns>返回的字符串</returns>
public static string CreatRequestGetResponse(string URL, string PostData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.CookieContainer = Cook;
myRequest.KeepAlive = true;
myRequest.Method = "POST";
string postdata = PostData;
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
byte[] postarr = encode.GetBytes(postdata);
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postarr.Length;
Stream outStream = myRequest.GetRequestStream();
outStream.Write(postarr, 0, postarr.Length);
outStream.Close();
WebResponse myResponse = myRequest.GetResponse();
StreamReader SReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
return SReader.ReadToEnd();
}
用的时候用CreatRequestGetResponse("目标网址","要传送的postdata")
比如<%=CreatRequestGetResponse("www.sina.com.cn","") %>
那你直接把这段方法作为runat =server 的脚本扔到aspx里面不就行了?
抱歉,我只授人以渔,不授人以鱼。关键方法都放出来了,如果你连runat=server脚本怎么用都不会,我劝你还是重新学学asp.net再写程序......
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询