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