errors running HttpWebRequest.GetResponse - 401 unauthorized

  • Thread starter Thread starter Steve Richter
  • Start date Start date
S

Steve Richter

I have a page that uses simple HTTP GET to do an ISBN lookup via
Amazon.com. The page works when I run it from //localhost. But I have
moved it to my godaddy.com shared hoster site, and I get errors on the
HttpWebRequest.GetResponse statement.

The remote server returned an error: (401) Unauthorized

also, when I use the network credentials object in the context of my
request, I get this error:

The underlying connection was closed: The server committed
an HTTP protocol violation.

Here is the web page I have prepared as I do battle with the godaddy
techs.
www.autocoder.com/demosite/AmazonQuery.aspx

click away at it, I dont think any harm can be done.

A code snippet follows. I am working under the assumption that the
problem resides with the config of the godaddy servers. Is there
something that has to be enabled in order for my asp.net code to send a
web request to another site? Are there magic words I can use to
convince the godaddy techs to this affect?

thanks,

-Steve

private void WebRequestTest( string InURL )
{
string line ;
StringBuilder sb = new StringBuilder( ) ;
HttpWebResponse resp ;
HttpWebRequest rqs = (HttpWebRequest)WebRequest.Create( InURL ) ;
rqs.MaximumAutomaticRedirections = 4;
rqs.MaximumResponseHeadersLength = 4;
// either user default credentials or the credentials specified on the
page.
if ( CheckBox1.Checked == false )
rqs.Credentials = CredentialCache.DefaultCredentials;
else
rqs.Credentials = new NetworkCredential( tbUser.Text,
tbPassword.Text, tbDomain.Text ) ;

// send the request to the remote site, get the response.
resp = (HttpWebResponse)rqs.GetResponse( ) ;
 
this error was corrected by godaddy providing me with a url of a proxy
server to use on my web requests:

HttpWebResponse resp = null ;
HttpWebRequest rqs = (HttpWebRequest)WebRequest.Create( InURL ) ;
string ProxyUrl = ConfigurationSettings.AppSettings["WebRequestProxy"]
;
if ( ProxyUrl != null )
{
WebProxy proxy = new WebProxy( ProxyUrl ) ;
rqs.Proxy = proxy ;
}
resp = (HttpWebResponse)rqs.GetResponse( ) ;

-Steve
 
It sounds like you're using 1.1... If so, try this in your config file:

<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

It seems most servers break the RFC and use spaces in their headers.
useUnsafeHeaderParsing fixed the problem for me...

-shane
 
Back
Top