简体   繁体   English

WebRequest.Create(“ URL”)的可能方式总是失败?

[英]Possible ways of WebRequest.Create(“URL”) always failed?

Currently in my website, I need to verify a URL param to ensure that it's a valid and it's an accessible URL. 当前在我的网站上,我需要验证URL参数以确保它是有效的并且是可访问的URL。 I'm using the code below: 我正在使用以下代码:

 var request = WebRequest.Create(url) as HttpWebRequest;
 if (request == null) return false;
     request.Method = "HEAD";
     using (var response = (HttpWebResponse)request.GetResponse())
     {
         return response.StatusCode == HttpStatusCode.OK;
     }

I had deployed the website in my server and so far every work fine, no issue with the URL verification. 我已将网站部署在服务器中,到目前为止一切正常,URL验证没有问题。

Now I had published the same application in client server. 现在,我已经在客户端服务器中发布了相同的应用程序。 But the URL verification always failed even just for http://www.google.com . 但是,即使对于http://www.google.com,URL验证也总是失败。 I have log file that record the failure of verification and the value. 我有记录验证失败和值的日志文件。

I retrieve all those URL value in client site and test again in my own server but no issue at all. 我在客户端站点中检索了所有这些URL值,然后在我自己的服务器中再次进行了测试,但没有任何问题。

That's why I suspect that it's not code issue but environment issue. 这就是为什么我怀疑这不是代码问题,而是环境问题。

Any advice on what to check? 有什么建议检查什么?

Thanks 谢谢

Try the following. 尝试以下方法。 If there is an exception, hopefully it will give you a clue on the issue you are having. 如果有例外,希望它将为您提供有关所遇到问题的线索。

var request = WebRequest.Create(url) as HttpWebRequest;
if (request == null) return false;
 request.AllowRedirect = false;  // default: true
 request.Method = "GET";   // this is the default

 try
  {
    var response = (HttpWebResponse)request.GetResponse());
    if (res.StatusCode == HttpStatusCode.OK)
        return true;
    else
       return false;
  }
 catch (Exception ex)
  {
     return ex.Message;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM