views:

218

answers:

2

I am trying to access a REST webservice, however my code doesn't work and keeps throwing a HTTP 401 error

see this example screen cap from Wireshark of the initial ssl request:

image link

This is from a Curl call to the same web address with the same details. The issue is that under my VB.NET version the "Extension: server_name" (and related) part of the SSL packet is missing, I believe that this is causing the server to not reply with the Server key exchange in the next couple packets.

Below is the VB.NET code

    Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(UrlString), HttpWebRequest)
    webRequest.Method = "GET"
    webRequest.ContentType = "application/x-www-form-urlencoded"

    webRequest.Credentials = New NetworkCredential(username, password)

    Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)

This fails on the last line (HTTP 401 error), issue does not seem to be related to my proxy and the user credentials are correct for the server as this request works from Curl and wget.

I have already tried setting the AuthenticationLevel as well as manually setting the Authorization header, and PreAuthenticate doesn't seem to change this issue either.

+1  A: 

Use the following to create a system.net trace log for your application and see what it shows.

feroze
what should I be looking for, all it really says to me is HTTP401 error as I said in the OP. I'm not sure what I should be looking for
Seph
A: 

The answer was that the WebService was not replying with a valid HTTP401 WWW-Authenticate header, instead was just returning with code like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
    <ErrorCode>NO_SUITABLE_AUTHENTICATION_METHOD</ErrorCode>
    <ErrorDescription>No suitable active authentication mechanism found to authorise request - authorise using supported mechanisms</ErrorDescription>
    <RequestID>gcqsrtvn</RequestID>
    <SystemTime>2010-08-12T09:14:21.831+10:00</SystemTime>
</Error>

So I had to manually add the authentication header as shown here: http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html

code:

Dim authBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}", username, password).ToCharArray())
webRequest.Headers("Authorization") = "Basic " + Convert.ToBase64String(authBytes)
Seph
This doesnt seem to make sense. In your question, you wrote that you are getting a 401 back, and that you tried adding the authorization header yourself and it didnt work. So, why is it working now? Also, if you just want to send authorization header in the first request itself, you can just set PreAuthenticate=true, and set the credentials on the CredentialCache with "Basic" as the auth method. **** Anyway like I said if you get a trace with the instructions I gave, I can tell you what is going on. It is not legal for a server to expect an Authorization header, yet not challenge the client.
feroze
the 401 i'm getting back does not fit the standard and it does not include any auth header information.As for why it didn't work before, that was due to an encoding issue and the code I was using for the manual header. Also setting PreAuthenticate did not work in this instance, the service I am connecting to is very sloppy and likes things done in a very specific way... but there is no documentation on how.Legal or not doesn't matter, it doesn't do it and that which was my issue.
Seph