Results 1 to 14 of 14

Thread: [2005]About WebClient & Socket

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005]About WebClient & Socket

    Yes you can. On a connected socket, call the GetStream method and use your StreamReader on the returned stream.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005]About WebClient & Socket

    Quote 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

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005]About WebClient & Socket

    Quote 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

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]About WebClient & Socket

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    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

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2005]About WebClient & Socket

    double post sorry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width