Basic authentication Fails

K

kpg*

Hi all,

In a web service I make a call to a remote server using
basic authentication.

On my development machine it works fine, but on the production
server I get an exception:

"The request was aborted: The request was canceled."

when I make this call:

Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

The request (as far as I can tell) is not making it to the
remote server. If I don't add the basic auth credentials
then the remote server does respond with an authorization
failure, as I would expect.

What I don't understand is why its acting different on the
development (it works) and production (it fails) boxes.

I'm sure its some security setting on the server (2003 Standard)
that is not on development (xml pro).

Any Ideas?

kpg

Here is the code:

Private Function doRequest(ByVal sURL As String, ByVal sMethod As String,
Optional ByVal sData As String = "", Optional ByVal UserName As String =
"", Optional ByVal Password As String = "") As String

Dim sResponse As String = ""

Try

Dim myWebRequest As WebRequest = WebRequest.Create(sURL)

If UserName.Length <> 0 Then
Dim credCache As CredentialCache = New CredentialCache()
credCache.Add(myWebRequest.RequestUri, "Basic", New
NetworkCredential(UserName, Password))
myWebRequest.Credentials = credCache
End If

myWebRequest.Method = sMethod.ToUpper
myWebRequest.ContentType = "application/x-www-form-urlencoded"

If myWebRequest.Method = "POST" Then
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(sData)
' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length

Dim myRequestStream As Stream =
myWebRequest.GetRequestStream()
myRequestStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
myRequestStream.Close()
Else
Throw New Exception("Method must be POST")
End If

' Assign the response object of 'WebRequest' to a
'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

Dim myResponseStream As Stream =
myWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(myResponseStream)
Dim readBuff(256) As [Char]

Dim count As Integer = streamRead.Read(readBuff, 0, 256)
While count > 0
Dim outputData As New [String](readBuff, 0, count)
sResponse &= outputData
count = streamRead.Read(readBuff, 0, 256)
End While

' Close the Stream Object.
myResponseStream.Close()
streamRead.Close()

' Release the HttpWebResponse Resource.
myWebResponse.Close()

Catch wex As WebException
sResponse = buildErrorResponse(wex.Message)
End Try

WriteToLog("Exit doRequest")

Return sResponse

End Function
 
B

bruce barker

go to the server and use IE to test if connection works. usually servers are
blocked from accessing remote sites. are you using a proxy?

-- bruce (sqlwork.com)


kpg* said:
Hi all,

In a web service I make a call to a remote server using
basic authentication.

On my development machine it works fine, but on the production
server I get an exception:

"The request was aborted: The request was canceled."

when I make this call:

Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

The request (as far as I can tell) is not making it to the
remote server. If I don't add the basic auth credentials
then the remote server does respond with an authorization
failure, as I would expect.

What I don't understand is why its acting different on the
development (it works) and production (it fails) boxes.

I'm sure its some security setting on the server (2003 Standard)
that is not on development (xml pro).

Any Ideas?

kpg

Here is the code:

Private Function doRequest(ByVal sURL As String, ByVal sMethod As String,
Optional ByVal sData As String = "", Optional ByVal UserName As String =
"", Optional ByVal Password As String = "") As String

Dim sResponse As String = ""

Try

Dim myWebRequest As WebRequest = WebRequest.Create(sURL)

If UserName.Length <> 0 Then
Dim credCache As CredentialCache = New CredentialCache()
credCache.Add(myWebRequest.RequestUri, "Basic", New
NetworkCredential(UserName, Password))
myWebRequest.Credentials = credCache
End If

myWebRequest.Method = sMethod.ToUpper
myWebRequest.ContentType = "application/x-www-form-urlencoded"

If myWebRequest.Method = "POST" Then
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(sData)
' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length

Dim myRequestStream As Stream =
myWebRequest.GetRequestStream()
myRequestStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
myRequestStream.Close()
Else
Throw New Exception("Method must be POST")
End If

' Assign the response object of 'WebRequest' to a
'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

Dim myResponseStream As Stream =
myWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(myResponseStream)
Dim readBuff(256) As [Char]

Dim count As Integer = streamRead.Read(readBuff, 0, 256)
While count > 0
Dim outputData As New [String](readBuff, 0, count)
sResponse &= outputData
count = streamRead.Read(readBuff, 0, 256)
End While

' Close the Stream Object.
myResponseStream.Close()
streamRead.Close()

' Release the HttpWebResponse Resource.
myWebResponse.Close()

Catch wex As WebException
sResponse = buildErrorResponse(wex.Message)
End Try

WriteToLog("Exit doRequest")

Return sResponse

End Function
 
K

kpg*

I love answering my own questions. It makes me feel smart.

I changed this:

Dim myWebRequest As WebRequest = WebRequest.Create(sURL)

to this:

Dim myWebRequest As HttpWebRequest = HttpWebRequest.Create(sURL)
myWebRequest.KeepAlive = False

and it works on the production server now.


I don't recall why I originally used WebRequest insted of
HttpWebrequest, but WebRequest does not expose the keepalive
property.

however...

Is the reason this works becuase the 2003 server box has
a differnet default timeout than my xml pro box?

Even so, without basic auth the request went through, so
hwat's different with basic auth?

As you can see, I don't understand why this works...

kpg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top