使用c#读取网站内容时出错

时间:2011-11-03 17:38:35

标签: c#

我正在尝试使用C#获取代码中的网页内容,但它给了我错误。请帮我解决。

  string url = "http://www.abesoft.org/query.asp
                searchtype=ANY&
                query_param=USDOT&original_query_param=NAME&
                query_string=2134430&original_query_string=NATIONAL GRASS LLC";

 WebRequest request = WebRequest.Create(url);
 WebResponse response = request.GetResponse();
 Stream data = response.GetResponseStream();
 string html = String.Empty;
 using (StreamReader sr = new StreamReader(data))
 {
      html = sr.ReadToEnd();
 }

错误 - "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

2 个答案:

答案 0 :(得分:2)

您可以使用:

 ServicePointManager.CertificatePolicy = delegate { return true; };

忽略证书错误。来源是这篇文章:

http://www.west-wind.com/weblog/posts/2011/Feb/11/HttpWebRequest-and-Ignoring-SSL-Certificate-Errors

基本上你只是试图忽略该网站上的错误证书。

答案 1 :(得分:1)

试试这个:

 string url =@"http://www.safersys.org/query.asp?
                searchtype=ANY&query_type=queryCarrierSnapshot&
                query_param=USDOT&original_query_param=NAME&
                query_string=2134430&original_query_string=NATIONAL GRASS LLC";

 HttpWebRequest request =  (HttpWebRequest) HttpWebRequest.Create(url);
 request.AllowAutoRedirect = false;

 WebResponse response = request.GetResponse();
 Stream data = response.GetResponseStream();
 string html = String.Empty;
 using (StreamReader sr = new StreamReader(data))
 {
      html = sr.ReadToEnd();
 }