客户端的检测 利用js脚本<script>function isExist(url) {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")xmlhttp.open("GET",url,false)xmlhttp.send()if(xmlhttp.status==200)alert(url+"存在")elsealert(url+"不存在")}</script><input type="button" οnclick="isExist('http://www.sina.com')"> 这样有时因为安全方面的问题 经常会提示 用户需要继续操作服务端检测 利用 HttpWebRequest和HttpWebResponse 当访问的url需要访问者的认证信息时 private bool doCheckReportServerUrl(string sUrl, string sUserName, string sUserPassword) { bool value = false; if (sUrl == null || sUrl.Length == 0) return value;
System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(sUrl); httpWebRequest.Credentials = new NetworkCredential(sUserName, sUserPassword);
System.Net.HttpWebResponse httpWebResponse; string strReturn="";
try { httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); System.IO.Stream streamResponse = httpWebResponse.GetResponseStream(); System.IO.StreamReader streamReader = new StreamReader(streamResponse, System.Text.Encoding.Default);
char[] readBuff = new char[256]; streamReader.Read(readBuff,0,256); strReturn = new string(readBuff, 0, 256); value = true; } catch (System.Net.WebException ex) { throw new WebException(GetResourceObject("Conform_WrongReportServerUrl_Text",null)); } return value; }HttpWebRequest和HttpWebResponse的应用 获取某个url的内容public string getPageFromURL(string url){ string content = ""; // Create a new HttpWebRequest object.Make sure that // a default proxy is set if you are behind a fure wall.//其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的Create方法来创建. HttpWebRequest myHttpWebRequest1 =(HttpWebRequest)WebRequest.Create(url);
//不维持与服务器的请求状态 myHttpWebRequest1.KeepAlive=false;//创建一个HttpWebRequest对象 //Assign the response object of HttpWebRequest to a HttpWebResponse variable./ HttpWebResponse myHttpWebResponse1; try { //根据微软MSDN上所说:"决不要直接创建HttpWebResponse的实例,要使用HttpWebRequest的GetResponse()方法返回的实例."具体的原因我也不清楚,可能HttpWebResponse类的构造函数中没有实现HttpWebResponse实例的代码吧. myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse(); //设置页面的编码模式 System.Text.Encoding utf8 = System.Text.Encoding.Default; Stream streamResponse=myHttpWebResponse1.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse, utf8);
Char[] readBuff = new Char[256]; //这里使用了StreamReader的Read()方法,参数意指从0开始读取256个char到readByff中. //Read()方法返回值为指定的字符串数组,当达到文件或流的末尾使,方法返回0 int count = streamRead.Read( readBuff, 0, 256 ); while (count > 0) { String outputData = new String(readBuff, 0, count); content += outputData; count = streamRead.Read(readBuff, 0, 256); } myHttpWebResponse1.Close(); return(content); } catch(WebException ex) { content = "在请求URL为:" + url + "的页面时产生错误,错误信息为" + ex.ToString(); return(content); }}
HttpWebRequest、HttpWebResponse获取网页中文乱码解决方案
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addr); // Change Username and password. //request.Credentials = new NetworkCredential("[username]", "[password]");
// Downloads the XML file from the specified server. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.CharacterSet); Console.WriteLine("please input charset:"); addr = Console.ReadLine(); //System.IO.BufferedStream bf = new BufferedStream(response.GetResponseStream); System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(addr)); Console.Write(sr.ReadToEnd()); sr.Close(); response.Close();HttpWebRequest、HttpWebResponse和HTTP 协议HttpWebRequest 是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过 HTTP 协议和服务器交互。
HttpWebRequest 对 HTTP 协议进行了完整的封装,对 HTTP 协议中的 Header, Content, Cookie 都做了属性和方法的支持,很容易就能编写出一个模拟浏览器自动登录的程序。
程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式进行一下说明:
1. GET 方式。 GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.google.com/webhp?hl=zh-CN 中,前面部分 http://www.google.com/webhp 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下:
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.google.com/webhp?hl=zh-CN" );req.Method = "GET";using (WebResponse wr = req.GetResponse()){ //在这里对接收到的页面内容进行处理}
2. POST 方式。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。程序代码如下:
string param = "hl=zh-CN&newwindow=1";byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.google.com/intl/zh-CN/" );req.Method = "POST";req.ContentType = "application/x-www-form-urlencoded";req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream()){ reqStream.Write(bs, 0, bs.Length);}using (WebResponse wr = req.GetResponse()){ //在这里对接收到的页面内容进行处理}
在上面的代码中,我们访问了 www.google.com 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。
3. 使用 GET 方式提交中文数据。 GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:
Encoding myEncoding = Encoding.GetEncoding("gb2312");string address = "http://www.baidu.com/s?" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);req.Method = "GET";using (WebResponse wr = req.GetResponse()){ //在这里对接收到的页面内容进行处理}
在上面的程序代码中,我们以 GET 方式访问了网址 http://www.baidu.com/s ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。常见的网站中, www.baidu.com (百度)的编码方式是 gb2312, www.google.com (谷歌)的编码方式是 utf8。
4. 使用 POST 方式提交中文数据。 POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。用 gb2312 方式编码访问的程序代码如下:
Encoding myEncoding = Encoding.GetEncoding("gb2312");string param = HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "&" + HttpUtility.UrlEncode("参数二", myEncoding) + "=" + HttpUtility.UrlEncode("值二", myEncoding);
byte[] postBytes = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.baidu.com/s" );req.Method = "POST";req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";req.ContentLength = postBytes.Length;
using (Stream reqStream = req.GetRequestStream()){ reqStream.Write(bs, 0, bs.Length);}using (WebResponse wr = req.GetResponse()){ //在这里对接收到的页面内容进行处理}
从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。
以上列出了客户端程序使用 HTTP 协议与服务器交互的情况,常用的是 GET 和 POST 方式。现在流行的 WebService 也是通过 HTTP 协议来交互的,使用的是 POST 方法。与以上稍有所不同的是, WebService 提交的数据内容和接收到的数据内容都是使用了 XML 方式编码。所以, HttpWebRequest 也可以使用在调用 WebService 的情况下。