PC Review


Reply
Thread Tools Rate Thread

Basic authentication Fails

 
 
kpg*
Guest
Posts: n/a
 
      5th Mar 2008
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



 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      5th Mar 2008
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*" wrote:

> 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
>
>
>
>

 
Reply With Quote
 
kpg*
Guest
Posts: n/a
 
      5th Mar 2008
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
 
Reply With Quote
 
kpg*
Guest
Posts: n/a
 
      5th Mar 2008
> my xml pro box

I seem to keep typing xml pro.

I mean to say 'XP Pro'

geez...
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forms Authentication displays basic authentication dialogue window Brett Porter Microsoft ASP .NET 5 3rd Feb 2004 07:06 PM
Basic Authentication v. Integrated Windows Authentication w/ Delegation Mark Microsoft ASP .NET 0 20th Jan 2004 03:13 PM
ASP.Net Forms authentication with basic authentication popup Brett Porter Microsoft ASP .NET 2 20th Jan 2004 02:17 PM
Forms Authentication Fails Between ASP.NET 1.0 and 1.1 Applications (Cookie Decryption Fails) John Saunders Microsoft ASP .NET 1 18th Nov 2003 03:25 PM
401 Authentication fail calling Web Service over SSL from PocketPC with Basic Authentication. John Hynes Microsoft Dot NET Compact Framework 0 19th Sep 2003 02:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:12 AM.