C# REST服务切换到https,在浏览器中工作,抛出一个;(403)禁止;c中的错误#

C# REST服务切换到https,在浏览器中工作,抛出一个;(403)禁止;c中的错误#,c#,rest,http-status-code-403,weather-api,noaa,C#,Rest,Http Status Code 403,Weather Api,Noaa,NOAA最近将他们的服务从http切换到https,一个已经运行多年的c#呼叫现在返回“远程服务器返回错误:(403)禁止” 奇怪的是,同样的电话在浏览器和邮递员身上都能正常工作。为什么服务器会拒绝一个请求而不是另一个,我缺少什么 网址: 根据下面接受的答案修改样本代码。这两个版本从未设置UserAgent,显然现在需要: string xml = ""; string url = ""; try { using (System.Net.WebClient wc = new System

NOAA最近将他们的服务从http切换到https,一个已经运行多年的c#呼叫现在返回“远程服务器返回错误:(403)禁止”

奇怪的是,同样的电话在浏览器和邮递员身上都能正常工作。为什么服务器会拒绝一个请求而不是另一个,我缺少什么

网址:

根据下面接受的答案修改样本代码。这两个版本从未设置UserAgent,显然现在需要:

string xml = "";
string url = "";
try
{
    using (System.Net.WebClient wc = new System.Net.WebClient())
    {
        url = "https://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?zipCodeList=44113&product=time-series&begin=2017-03-23T00:00:00&temp=temp&appt=appt&pop12=pop12";
        wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 1.1.4322; .NET CLR 3.5.20404)");
        xml = wc.DownloadString(new Uri(url));
    }
    //......  
}
catch (Exception ex)
{
    LogError(ex);
}
还是这个

string xml = ""; 
string url = "";
url = "https://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?zipCodeList=44113&product=time-series&begin=2017-03-23T00:00:00&temp=temp&appt=appt&pop12=pop12";
HttpWebRequest httpWR = (HttpWebRequest)WebRequest.Create(url);
httpWR.Method = WebRequestMethods.Http.Get;
httpWR.Accept = "application/xml";
httpWR.UserAgent = ".NET Framework Client";
try
{
    using (HttpWebResponse response = (HttpWebResponse)httpWR.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            xml = reader.ReadToEnd();
        }
    }
    //......
}
catch (Exception ex)
{
    LogError(ex);
}

我认为该服务需要一个有效的用户代理

我已经修改了你的代码来包含它

using (System.Net.WebClient wc = new System.Net.WebClient())
        {
            url = "https://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?zipCodeList=44113&product=time-series&begin=2017-03-23T00:00:00&temp=temp&appt=appt&pop12=pop12";

            wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 1.1.4322; .NET CLR 3.5.20404)");


            xml = wc.DownloadString(url);
        }

@我已经测试并更新了我的答案。它可以工作。@Plamen Kouzov请在调用
wc.DownloadString(url)
之前先调用用户代理,我已经让它工作了,没有任何错误。您现在遇到了什么错误?我有ServicePointManager.ServerCertificateValidationCallback=delegate{return true;};就在xml=wc.DownloadString(新Uri(url))之前;“我想知道他们是否出于某种原因没有将我们列入黑名单。”普拉门·库佐夫:哦,不!请刷新页面。后来我把答案改成了别的。最初的那个是错的,太好了!我希望我能多投几次赞成票。