|
-
Jan 26th, 2015, 01:45 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] websocket
Hi,
Im trying to use the following socket code to send an http request and get a reply from google. However nothing is coming back, the app just freezes.
Any idea where i'm doing it wrong thanks?
vb Code:
sender2.Connect("google.com", 80) TextBox2.Text = ("Socket connected to {0}" & sender2.RemoteEndPoint.ToString()) ' Encode the data string into a byte array. Dim msg As String = ("POST / HTTP/1.1 \r\n" & Environment.NewLine & _ "Host: google.com \r\n" & Environment.NewLine & _ "Accept: */* \r\n>" & Environment.NewLine & _ "Content-Type: text/html \r\n" & Environment.NewLine & _ "Content-Length: 0 \r\n" & Environment.NewLine) ' Send the data through the socket. Dim bytesSent As Integer = sender2.Send(Encoding.ASCII.GetBytes(msg)) TextBox1.Text = msg ' Receive the response from the remote device. Dim bytesRec As Integer = sender2.Receive(bytes) TextBox2.Text = ("Echoed test = {0}" & Encoding.ASCII.GetString(bytes, 0, bytesRec)) ' Release the socket. sender2.Shutdown(SocketShutdown.Both) sender2.Close() sender2 = Nothing
Thanks
-
Jan 26th, 2015, 05:44 PM
#2
Re: websocket
Unfortunately I know nothing about accessing Web pages, but I do wonder about the strings.
It looks like this may have come from a C based language, with string formatting escape characters (e.g. "\r\n" for Carriage Return and LineFeed) embedded in the strings, and then you add them manually with '& Environment.NewLine'.
My assumption is you wouldn't want the "\r\n" in the strings since VB is not going to interpret then and add the Return and Linefeed characters in their place.
Try removing them from the strings and see if it helps.
-
Jan 26th, 2015, 06:14 PM
#3
Re: websocket
The webclient and http web request class was made for a reason.
-
Jan 26th, 2015, 07:33 PM
#4
Thread Starter
Frenzied Member
Re: websocket
@passel: I tried your suggestion , but no luck, thc tho
@Toph: If i use http request i'm pretty sure it is gonna work. But if possible i would like to stick to the current socket code.
more info :
if I submit with the following header
Code:
Host: google.com
Accept: */*
Content-Type: text/html
Content-Length: 0
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36
I get a 400 bad reqest msg "Your browser sent a request that this server could not understand."
but if I try to add an "GET" or "POST" infront nothing comes back . anything special about the two ?
thanks
-
Feb 3rd, 2015, 04:55 PM
#5
Thread Starter
Frenzied Member
Re: websocket
I got it to work
First I removed \r\n and only used with Environment.NewLine as suggested by passel
Then I was only missing an additional Environment.NewLine at the end of my request.
-
Feb 3rd, 2015, 05:00 PM
#6
Re: [RESOLVED] websocket
The application freezes because you are performing the actions on the UI thread. I can't see any reason why you need the direct Httpwebrequest over the http wrapped webclient. If you don't want the UI to freeze then don't perform the action on the ui thread.
-
Feb 3rd, 2015, 05:03 PM
#7
Thread Starter
Frenzied Member
Re: [RESOLVED] websocket
@ident
actually the reason it was freezing, was because i didn't send the end of request signal (Environment.NewLine), as a result the server was waiting for me to receive the next request element. (i normally don't expect a simple http head request to take more than 1 sec)
-
Feb 3rd, 2015, 05:05 PM
#8
Re: [RESOLVED] websocket
Oh right you must be the first person to perform a heavy load request on the UI with out freezing.
-
Feb 3rd, 2015, 05:06 PM
#9
Re: [RESOLVED] websocket
How can you expect a time limit? It's up to the server load you are connecting to.
-
Feb 3rd, 2015, 05:10 PM
#10
Thread Starter
Frenzied Member
Re: [RESOLVED] websocket
Yes, it's up to the server, but you would expect the response time to be reasonable loll.... And that's probably why they invented the TTL thing to handle this kind of situation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|