473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 9396
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IIS的响应时间问题,急!!!多谢无 | last post by:
i distribute a website on IIS which is programmed with C# HttpHandler,then i pragram a Winform client to send empty call to the website and record the time that the call return to client. when the...
3
by: Troy | last post by:
I have a class library (DLL) that I created and have been using for sometime in my various .NET applications. Recently, I have been having problems with what I believe is corruption (or a bug) of the...
0
by: IIS的响应时间问题,急!!!多谢无 | last post by:
i distribute a website on IIS which is programmed with C# HttpHandler,then i send empty call to the website and record the time that the call return to client. 1、when the client is C# Winform,...
2
by: dotnettester | last post by:
Hi, I am using Response.writefile to write a file into clients browser. It works fine with files upto 60MB. When I come across a file size bigger than that, it break with error saying page can...
12
by: Britney | last post by:
Hi guys, in my default.aspx file, I have following code. when I go to browser, I enter values in both textboxs, then I hit submit Button, however, it didn't go to page2.aspx. instead, it was...
3
by: idikoko | last post by:
I have a small problem I am trying to extract charcters from one string store it in another and then compare the new string and out a msg. The code looks fine but it always runs into a compiler...
1
by: James | last post by:
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.. ...
5
by: KusoYumi | last post by:
Hello, im facing date and time problem. I want get a time in Date Type, with this format HH:mm:SS i try to get new Date(), then format it as String. Then i use substring to get the time. Then i...
3
by: Rainy | last post by:
Hello! I'm having some trouble with pyserial package, I'm sending commands and reading responses from a custom pcb, and sometimes I get a proper response, at other times I get nothing, and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.