Solved

httpwebrequest hangs application

Posted on 2009-05-08
18
304 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
Comment Utility
Have you tried System.Net.Webclient?
I believe its a wrapper on HttpWebRequest/Response.
0
 

Author Comment

by:Yogesh_Agarwal
Comment Utility
but that doesnt have useragent, timeout....
0
 
LVL 1

Expert Comment

by:sfun28
Comment Utility
0
 
LVL 1

Expert Comment

by:sfun28
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
can you post code?
0
 

Author Comment

by:Yogesh_Agarwal
Comment Utility

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:sfun28
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it tought me a good lesson..
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now