在生产环境中运行时从远程服务器获取"401未经授权" [英] Getting '401 Unauthorized' from remote Server when ran in Production

查看:172
本文介绍了在生产环境中运行时从远程服务器获取"401未经授权"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用WebRequest将XML数据发送给公司.当我从本地计算机运行该应用程序时,它可以完美运行.但是,一旦将相同的代码推广到生产环境,充其量就是充其量.很多时候它只是失败了,而我得到的错误消息是远程服务器返回了一个错误:(401)未经授权".有时我也会收到超时消息.有任何想法吗?这是令人反感的代码:

Hi All,

I''m sending XML data to a company using a WebRequest. When I run the application from my local machine it works flawlessly. However, once I promote the same code to production It''s spotty at best. A lot of times it simply fails and the error message I get is "The remote server returned an error: (401) Unauthorized". Sometimes I get a timeout message as well. Any ideas? Here is the offending code:

private XmlUtil GetXMLResponse(XmlUtil xmlreq)
{
    string url = SystemConfig.SilverPopAPIURL + JSession + "?xml=" + xmlreq.OuterXml;

    _TransactionLog.AppendFormat("{0} : sent ''{1}'' to SilverPop,\r\n", DateTime.Now, url);

    WebRequest request = WebRequest.Create(url);
    try
    {
        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                XmlUtil rtn = new XmlUtil(reader.ReadToEnd());

                _TransactionLog.AppendFormat("{0} : received ''{1}'' from SilverPop,\r\n", DateTime.Now, rtn.OuterXml);

                return rtn;
            }
        }
    }
    catch (Exception ex)
    {
        _TransactionLog.AppendFormat("{0} : exception ''{1}'' with SilverPop,\r\n", DateTime.Now, ex.Message);
        throw;
    }
}

推荐答案

正如它所说的,这是因为您未被授权.尝试在Create ....
之后粘贴此行.
That, as it says, is because you are not authorised. Try sticking this line in after the Create....

request.Credentials = CredentialCache.DefaultCredentials;


这篇关于在生产环境中运行时从远程服务器获取"401未经授权"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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