By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.CreateFromCertFil e("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
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
=======================

"James" <ni*******@yahoo.com> wrote in message
news:65*************************@posting.google.co m...
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.CreateFromCertFil e("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

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.