URL代码的返回状态代码在C#中不起作用 [英] Return status code of URL code is not working in C#

查看:62
本文介绍了URL代码的返回状态代码在C#中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试获取URL的状态代码。我正在使用以下代码来实现它。它运作良好。但问题是,代码显示一些有效的URL也无法正常工作。



请参阅下面的代码,其中URL完全适用于浏览器,但代码返回以下结果:



远程服务器返回错误:(403)禁止



为什么?需要你的帮助来解决它。

提前致谢。



我尝试过的事情:



我试过不同的代码,但都没有给出准确的结果。

试试
{
string url =https://www.digikey.com/product-detail/en/rushup/CLOUD-JAM-L4/1770-1003-ND/7388464;
//为指定的URL创建HttpWebRequest。
HttpWebRequest myHttpWebRequest =(HttpWebRequest)WebRequest.Create(url);
//发送HttpWebRequest并等待响应。
HttpWebResponse myHttpWebResponse =(HttpWebResponse)myHttpWebRequest.GetResponse();
if(myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine(\\\\ nResponse状态代码正常且StatusDescription为:{0},
myHttpWebResponse.StatusDescription );
//释放响应的资源。
myHttpWebResponse.Close();
}
catch(WebException e)
{
Console.WriteLine(\\\\ nnWebException Raised。发生以下错误:{0},e.Status) ;
}
catch(例外e)
{
Console.WriteLine(\ n以下异常被引发:{0},e.Message);
}

解决方案

尝试使用:

 HttpWebRequest myHttpWebRequest = (HttpWebRequest的)WebRequest.Create(URL); 
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
myHttpWebRequest.UserAgent =MyHttpRequester;



这将让应用程序使用登录用户的凭据访问该站点。


我认为您的问题是由url字符串中的双引号引起的,请勿使用:

 string url =< a href =https: //www.digikey.com\"\";

但使用:

 string url = @< a href =https:// www.digikey.com ; 


Hi Guys,

I am trying to get status code of URL. I'm using the below code to achieve it. It is working good. But problem is, The code is showing some valid URL's also not working.

Please see the code below where the URL perfectly working on Browser but code returning the below results:

The remote server returned an error: (403) Forbidden

Why? need your help to fix it.
Thanks in advance.

What I have tried:

I tried with different codes but all are not giving the accurate results.

try
{
    string url="https://www.digikey.com/product-detail/en/rushup/CLOUD-JAM-L4/1770-1003-ND/7388464";
    // Creates an HttpWebRequest for the specified URL. 
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
        Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
            myHttpWebResponse.StatusDescription);
    // Releases the resources of the response.
    myHttpWebResponse.Close();
}
catch (WebException e)
{
    Console.WriteLine("\r\nWebException Raised. The following error occured : {0}", e.Status);
}
catch (Exception e)
{
    Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
} 

解决方案

Try using this:

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
myHttpWebRequest.UserAgent = "MyHttpRequester";


This will let the application use the credentials of the logged in user to access the site.


I think your problem is caused by double quotes in the url string, do not use:

string url="<a href="https://www.digikey.com"";

But use:

string url = @"<a href=""https://www.digikey.com""";


这篇关于URL代码的返回状态代码在C#中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆