HTTPRequest Get Response Method and Time out problem with VB.NET

J

James

Hi,

I am new to .NET framework. I am trying to get data from the third
party web application using WebHTTPRequest and Response class. I am
getting TimeOut exception at GetResponse Method..

Dim objResponse As HttpWebResponse = objRequest.GetResponse()


tried out different solutions but still problem persist. Here is the
code from my test applicaiton

Thanks,
--James
=============================================================================
Private Function readHtmlPage(ByVal Url As String)

Dim result As String = ""
Dim strPost As String = txtRequest.Text
Dim myWriter As StreamWriter
Dim objRequest As HttpWebRequest = WebRequest.Create(Url)


objRequest.Method = "POST"
objRequest.ContentLength = strPost.Length
objRequest.ContentType = "application/x-www-form-urlencoded"

Dim c As X509Certificates.X509Certificate

Try
c = X509Certificates.X509Certificate.CreateFromCertFile("C:\MyFolder\xyz.cer")
objRequest.ClientCertificates.Add(c)
Debug.Write("Client cert added. Name: " & c.GetName)

myWriter = New StreamWriter(objRequest.GetRequestStream())
myWriter.Write(strPost, 0, strPost.Length)
Catch e As Exception
Return e.Message
txtResponse.Text = e.Message
Finally

'myWriter.Close()

End Try

'
Try

Dim objResponse As HttpWebResponse =
objRequest.GetResponse()
Dim sr As StreamReader
sr = New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()
sr.Close()

txtResponse.Text = result
Return result
Catch e As Exception
Return e.Message
End Try

End Function


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

txtResponse.Text =
readHtmlPage("https://mywebpage.com/myApplicaiton.exe")


End Sub
 
F

Feroze [msft]

There are a couple of issues with your code.

First, you are setting the content length = "strPost.Length" . But then you
are using a StreamWriter to write the data. Sometimes you could end up with
a mismatch between the lengths of data you set in the content-length, and
the data actually written by the StreamWriter, esp if the encodings dont
match.

What you should do is:

byte [] rawbytes = Encoding.ASCII.GetBytes(strPost);
req.ContentLength = rawbytes.Length;

...
Stream rq = req.GetRequestStream();
rq.Write(rawbytes,0,rawbytes.Length);
rq.Close();
...

Secondly, you are not closing the response object. Since the StreamReader
you attached to the response stream does not own the underlying stream, it
wont close the stream when you close the StreamReader. You must close the
stream, or just call response.Close().

feroze
=======================
this posting is provided as-is, it provides no guarantees and confers no
rights.
=======================
 

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

Top