Solved

httpwebrequest hangs application

Posted on 2009-05-08
18
305 Views
Last Modified: 2013-11-26
hi,

I have a function func. It is started by thread. that func calls another function nav which accepts a string. it uses httpwebrequest and response to get source of website. My Application works well if i use web browser control. it gets hanged when i use httpwebrequest. I am just creatign req, geting response and assigning the text to webbrowser.document text property...
0
Comment
Question by:Yogesh_Agarwal
  • 9
  • 9
18 Comments
 
LVL 1

Expert Comment

by:sfun28
ID: 24335906
Have you tried System.Net.Webclient?
I believe its a wrapper on HttpWebRequest/Response.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336034
but that doesnt have useragent, timeout....
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24336154
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24336171
anyways, it sounds like you're dealing with a thread issue.
have you tried calling nav without creating a new thread.  have you tested that it works in single-thread environment?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336280
no i din tried. but it has to be called only from thread.. i do want to call out of thread.. it will collide the logic  of my application..
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24336666
well...doing the wrong sorts of things with threads could certainly cause hanging - like if you had a deadlock scenario.  It sounds like you need more testing/debugging - its hard to say what's wrong with the information you've provided, it could really be anything.  I would start with a single thread and step through your code in the debugger.  Does it still hang?  If not, then something you're doing with threading is messing it up.  Isolate the problem down to the most basic case possible and work up from there.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24336814
there is no dead locks.. it works fine.. only during request and respnses it gets hang..  i am really confused that wat cud eb the problem..
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24342124
can you post code?
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24344973

dim t1 as new thread(addressof web_thread)

'Few Conditions

t1.start
 

sub web_thread()
 
 

Dim s As String = navigate(TextBox1.Text.ToString)

            If s Is Nothing Then

                Button1.PerformClick()

                Exit Sub

            Else

            

                WebBrowser1.DocumentText = s.ToString

         

            End If
 

end sub
 

Public Function Navigate(byval str as string) as String

 Try
 

            If pse = False Then

                Dim wreq As HttpWebRequest

                Dim wres As HttpWebResponse

                wreq = HttpWebRequest.Create(str)

                wreq.AllowAutoRedirect = True

                wres = wreq.GetResponse

                Dim sr As StreamReader

                sr = New StreamReader(wres.GetResponseStream)

                str = sr.ReadToEnd

                str = replacetags(str)

                Return str
 

            End If
 

            Return Nothing
 

        Catch ex As Exception
 

            Return Nothing

        End Try
 

End Function

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:sfun28
ID: 24349542
This worked fine for me (see code snippet below).  What is replacetags(str) doing?  is there some kind of recursion or other infinite loop?  have you walked this through the debugger?  what step does it get stuck on?
 Dim t1 As New Thread(AddressOf Navigate)
 

    Public Function Navigate() As String

        Try

            Dim str As String = "http://www.yahoo.com"
 

            Dim wreq As HttpWebRequest

            Dim wres As HttpWebResponse

            wreq = HttpWebRequest.Create(Str)

            wreq.AllowAutoRedirect = True

            wres = wreq.GetResponse

            Dim sr As StreamReader

            sr = New StreamReader(wres.GetResponseStream)

            Str = sr.ReadToEnd

            Return str
 

        Catch ex As Exception
 

            Return Nothing

        End Try
 

    End Function

Open in new window

0
 

Author Comment

by:Yogesh_Agarwal
ID: 24349674
i don want to call navigate function through another thread.. Replace tags will remover certain tags in html.. and will return.. it s just one line of statement.. No problem in that.. HOw can i find that when UI Hangs through debugger?
0
 
LVL 1

Accepted Solution

by:
sfun28 earned 500 total points
ID: 24349719
You are calling navigate through a separate thread already.  I just cut out the web_thread() sub to isolate the web code.  web_thread calls Navigate on the same thread, the new one you created.  for debugging, just set a breakpoint at:
Dim s As String = navigate(TextBox1.Text.ToString)
please do this right now...just set a breakpoint and step through every line of code and tell me what happens.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24352090
i did.. wat happens there is,

Goes to function, Executes all line one by one. it stops at get response. UI comes. at that time UI is responsive.. after getting request ui goes and again execution starts line by line.. but wait.. When it comes second time, UI doesnt Shows up.. so it means that at second round UI Get hanged up.. Second time that web_thread functions is raised by Events(Button1.PerformClick()).. so now problem is that raising that function by events right?
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24354395
I'm having trouble understanding the flow of control here.  Button1.PerformClick() is only executed if navigate(TextBox1.Text.ToString) returns nothing.  so what do you mean by "second time that web_thread functions is raised by events(button1.performclick())?

What exactly is calling web_thread?  The code snippet you posted doesn't show that. I see
dim t1 as new thread(addressof web_thread)
which looks like a public instance varible, but how are you actually starting that thread? from where?

0
 

Author Comment

by:Yogesh_Agarwal
ID: 24355529
i am starting that thread when form load event fires.. then after completion of getting response and setting as document text, i am doing few manipulation in document Completed Even of Web browser.. then again i am firing botton1.performclick()     (webthread_1 handles that)...
0
 
LVL 1

Expert Comment

by:sfun28
ID: 24356589
I'm really having a hard time understanding you.  What do you mean when you say " i am doing few manipulation in document Completed Even of Web browser."

so you're telling me that you fire button1.performclick(), which fires web_thread (which itself would fire button1.performclick() if the string is empty?)

i'm still having trouble following your flow.  this sounds unnecessarily complex.  I'm sorry but I cannot afford any more time to help you debug this.  I strongly suggest you take a step back and look at your design.  It seems very complicated.
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24356629
i know that i fal to make u understand... how over i managed to solve me problem.. thanks a lot for spending ur precious time.. u helped me a lot...
0
 

Author Closing Comment

by:Yogesh_Agarwal
ID: 31579428
it tought me a good lesson..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
update textboxes with Streamreader 21 42
vb.net 2 37
Vb.net dynamic formulas in runtime 11 61
How to tell the version of visual studio used for a certain project. 3 24
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short film showing how OnPage and Connectwise integration works.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now