Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
    }
}
Posted

1 solution

That, as it says, is because you are not authorised. Try sticking this line in after the Create....

request.Credentials = CredentialCache.DefaultCredentials;
 
Share this answer
 
Comments
Orcun Iyigun 25-Aug-11 12:38pm    
Well that might fix it but also Try setting the credentials explicitly to a user account known to have the required access. DefaultCredentials might revert to say the Local Machine or Network Service account.
ehwash 25-Aug-11 15:52pm    
Unfortunately that did work. As always it worked on my machine. But on a co-worker's box who downloaded it it didn't work.

Here's a little more info. The XML should work from anywhere, even from a HTML page. Simply POST the correctly formatted XML to this third party and it should appear in your private database on there server. However, we are not getting consistent results. It works for me and sometime on production.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900