|
-
Nov 4th, 2008, 01:21 PM
#1
Thread Starter
Fanatic Member
[2005]About WebClient & Socket
Hi ,
i was using WebClient to read data from a webpage
now my question is can the socket do the same thing as webclient
which mean i can use New StreamReader to read a webpage...
has someone any example how to read data from a webpage using socket
Thanks
-
Nov 4th, 2008, 06:51 PM
#2
Re: [2005]About WebClient & Socket
If you want to use a StreamReader then just call the WebClient's OpenRead method. It returns a Stream that you can put a StreamReader on top of.
Of course, you could also call DownloadString and the put a StringReader on top of that. The StringReader gives you the same interface as a StreamReader but on top of a String instead of a Stream.
-
Nov 5th, 2008, 07:56 AM
#3
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
Hi John ,
yes i was using StreamReader with the WebClient's OpenRead method
what i ask is if i can use socket with StreamReader like Webclient
i dont want to use Webclient anymore
Thanks
-
Nov 5th, 2008, 08:01 AM
#4
Re: [2005]About WebClient & Socket
Yes you can. On a connected socket, call the GetStream method and use your StreamReader on the returned stream.
-
Nov 5th, 2008, 08:21 AM
#5
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
 Originally Posted by Atheist
Yes you can. On a connected socket, call the GetStream method and use your StreamReader on the returned stream.
Hi Atheist ,
do you have a simple example that use GetStream and StreamReader
on webclient i was doing like this :
Code:
Dim URL As String
Dim client As New WebClient()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
URL = "www.site.com"
Dim readerd As New StreamReader(client.OpenRead(URL))
Dim strreader As String
strreader = readerd.ReadToEnd
'do what you want
readerd.Close()
End Sub
Thanks
-
Nov 5th, 2008, 08:33 AM
#6
Re: [2005]About WebClient & Socket
The thing is that the webclient has alot of functionallity built in that you'll have to write yourself now. You dont just connect to a website and start reading data from it, you first have to sent a HTTP request to the server, then read the returned HTTP response, and parse the actual webpage data out of it, if any.
-
Nov 5th, 2008, 08:41 AM
#7
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
 Originally Posted by Atheist
The thing is that the webclient has alot of functionallity built in that you'll have to write yourself now. You dont just connect to a website and start reading data from it, you first have to sent a HTTP request to the server, then read the returned HTTP response, and parse the actual webpage data out of it, if any.
ok i see what you mean
no problem to rewrite code
if you have a simple example how to reading data it will be helpful
Thanks
-
Nov 5th, 2008, 08:50 AM
#8
Re: [2005]About WebClient & Socket
I dont have time to write an example right now, I could do later. Until then you should try yourself. Check out this list:
1. Do you know how to connect a System.Net.Sockets.Socket to a webserver?
2. Do you know what a HTTP request looks like?
3. Do you know how to send data through the Socket?
4. Do you know what a HTTP response looks like?
5. Do you know how to recieve data through the Socket?
Ask yourself these questions, if you find that you dont know any of these points, search on Google or MSDN.
-
Nov 5th, 2008, 08:58 AM
#9
Re: [2005]About WebClient & Socket
Hi Killer7k,
Here's another example of getting the source of the webpage using the WebRequest and WebResponse classes.
http://www.vbforums.com/showthread.php?t=449181#3
Atheist is right... why reinvent the wheel? Do you realise how much work is involved in creating a HTTP(S) client?
It's not always as simple as connect>request>receive ...
You will need to handle redirects and moved reponses things like that. What about HTTPS? Are you going to write a secure sockets layer module (or find one) to handle that?
What about the different versions of the HTTP protocol, will your code support 1.0, 1.1, both?
There certianly are scenerios where you need to create your own but just to get the source a web page there are quicker ways.
Last edited by the182guy; Nov 5th, 2008 at 09:04 AM.
Chris
-
Nov 5th, 2008, 09:27 AM
#10
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
Thank you Guy for your help
I know using Webclient will do much easier
but problem i'am getting with it is the : (500) Internal Server Error
so i tought using Socket can avoid me this problem
@the182guy ,
i saw your post and i have worked in the past with HttpWebRequest...
same things
Thanks
-
Nov 5th, 2008, 10:02 AM
#11
Re: [2005]About WebClient & Socket
The internal server error shouldnt depend on the WebClient or the HttpWebRequest class. Tell me, the site that gives you this error, can you navigate to it from your webbrowser?
-
Nov 5th, 2008, 09:43 PM
#12
Re: [2005]About WebClient & Socket
 Originally Posted by killer7k
Thank you Guy for your help
I know using Webclient will do much easier
but problem i'am getting with it is the : (500) Internal Server Error
so i tought using Socket can avoid me this problem
@the182guy ,
i saw your post and i have worked in the past with HttpWebRequest...
same things
Thanks 
You know, maybe you should have mentioned in the first post that you were getting an error when using the WebClient instead of wasting your time and everyone else's asking for something that will be a lot more work for you and us and won't even fix the problem. If you provide a full and clear explanation in the first place then we can provide the most appropriate solution. If you withhold relevant information then we cannot make an informed judgement on the best solution.
The whole point of a WebClient is to make common Web-related operations easier by doing a lot of the work for you. Do you supposed that perhaps the WebClient actually uses a Socket itself underneath to retrieve a Web page? I don't know if that is the case but it's highly likely.
-
Nov 6th, 2008, 04:35 PM
#13
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
Yes sorry it was my mistake i tought that using socket will solve the problem
the problem is when i navigate with brower to that page there is data there
but when i run that link usign Webclient i get a 500 internet server error
i dont really where the problem issue
-
Nov 6th, 2008, 04:39 PM
#14
Thread Starter
Fanatic Member
Re: [2005]About WebClient & Socket
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
|