Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

httpwebrequest hangs application

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
Yogesh_Agarwal
Asked:
Yogesh_Agarwal
  • 9
  • 9
1 Solution
 
sfun28Commented:
Have you tried System.Net.Webclient?
I believe its a wrapper on HttpWebRequest/Response.
0
 
Yogesh_AgarwalAuthor Commented:
but that doesnt have useragent, timeout....
0
 
sfun28Commented:
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
sfun28Commented:
can you post code?
0
 
Yogesh_AgarwalAuthor Commented:

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
 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
sfun28Commented:
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
 
Yogesh_AgarwalAuthor Commented:
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
 
Yogesh_AgarwalAuthor Commented:
it tought me a good lesson..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now