Solved

httpwebrequest hangs application

Posted on 2009-05-08
18
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell: ForEach-Object Export to CSV 4 92
Check only one toolstripmenu item 12 62
Removing Version of .NET in Windows 10 17 129
What are MicroServices? 4 68
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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