1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[C#] Loading webpage through proxy

Discussion in 'C, C++, C#' started by swordbeta, Aug 13, 2009.

  1. swordbeta

    swordbeta Newbie

    Joined:
    Aug 10, 2009
    Messages:
    12
    Likes Received:
    89
    I'm currently facing a problem, I'm a experienced PHP scripter and working on my C#, but I can't get this working.
    My real question was, if there's a cUrl libary which is similar to PHP's cUrl libary and easy to use?
    As I find it hard to use the WebRequest class (http://msdn.microsoft.com/en-us/library/debx8sh9.aspx)
    I will also need to POST.
     
    Last edited: Aug 13, 2009
  2. doomster

    doomster Junior Member

    Joined:
    Jun 24, 2009
    Messages:
    183
    Likes Received:
    58
    dude ur confusing me , in ur title you mention that u what to know Loading webpage through proxy and in the posy ur talking about webrequest class n post method , anyways if ur familer with winsock class in c# then u wont have any problem with both of them .
     
  3. doomster

    doomster Junior Member

    Joined:
    Jun 24, 2009
    Messages:
    183
    Likes Received:
    58
    am a blackhat prograammer , n winsock makes ur bot like 50 faster then normal webbrowser class bot . so my advice is go for winsock
     
  4. koolmelee

    koolmelee Newbie

    Joined:
    Aug 11, 2009
    Messages:
    4
    Likes Received:
    0
    I created a class that uses httpwebrequest to do this. It currently only works with certain web proxies at the moment(php base64 encoded url). If you are interested PM me and ill send you the code and full explanation on how to use it.

    I am also going to look into making it compatible with other proxy types and ip:port proxies and also adding winsock functionality.
     
  5. pyronaut

    pyronaut Executive VIP

    Joined:
    Dec 9, 2008
    Messages:
    1,229
    Likes Received:
    1,422
    Not familiar with PHP or Curl, but for Post. When i first learned C# i used this :

    Code:
    http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
    It certainly isnt perfect, and ive modified that code quite alot to allow for basic authentication and proxies. But its a good base.
     
  6. swordbeta

    swordbeta Newbie

    Joined:
    Aug 10, 2009
    Messages:
    12
    Likes Received:
    89
    Yes, I understand I am confusing you but I'm pretty sure my question was clear.
    Ok, thank you, I will try that.
    Looks like a good one, but I don't see proxy :(
     
  7. w84it

    w84it Regular Member

    Joined:
    Jun 18, 2009
    Messages:
    442
    Likes Received:
    752
    Just use the webclient if you are looking for a simple way.
    Here's a little snippet to get page using proxy.
    Code:
    Webclient wc = new Webclient();
     wc.proxy = "127.0.0.1:1337";
        string html = wc.downloadstring("[URL="http://www.google.com/"]http://www.google.com[/URL]");
    
     
  8. swordbeta

    swordbeta Newbie

    Joined:
    Aug 10, 2009
    Messages:
    12
    Likes Received:
    89
    Thank you, can I POST with that? Could you please give me a example?
    And how do I go from string to System.Net.IWebProxy?
     
    Last edited: Aug 14, 2009
  9. swordbeta

    swordbeta Newbie

    Joined:
    Aug 10, 2009
    Messages:
    12
    Likes Received:
    89
    Sorry, must have pressed the button too much o.0
     
    Last edited: Aug 14, 2009
  10. swordbeta

    swordbeta Newbie

    Joined:
    Aug 10, 2009
    Messages:
    12
    Likes Received:
    89
    Sorry, must have pressed the button too much o.0
     
    Last edited: Aug 14, 2009
  11. pyronaut

    pyronaut Executive VIP

    Joined:
    Dec 9, 2008
    Messages:
    1,229
    Likes Received:
    1,422
    With mine you need to incorporate your own proxy into it. But it should be fairly easy. Implementing the timeouts etc may be a bit harder. But you will get there.

    Just look at the MSDN library and you can pretty much copy and paste
     
  12. b4c5p4c3

    b4c5p4c3 Newbie

    Joined:
    May 4, 2009
    Messages:
    40
    Likes Received:
    39
    Here is a little snippet of code to do a complete post request in c#

    Code:
    //Construct your request
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("[URL]http://www.yoururl.com/[/URL]");
    //If you want proxy support 
    req.Proxy = new WebProxy("proxyurl", 8080);
    req.ContentType = "application/x-www-form-urlencoded";
    req.Accept = "image/gif, image/jpeg, */*";
    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0;";
    req.Referer = "[URL]http://www.refererurl.com/[/URL]";
    req.Method = "POST";
    //If you want to automagically follow 302 and 301 redirects set this to true
    req.AllowAutoRedirect = false;
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes("param1=value1&param2=value2");
    req.ContentLength = bytes.Length;
    //Make the real call
    Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    //Never forget to close the stream
    os.Close();
    //Get the response
    WebResponse resp = req.GetResponse();
    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string response = sr.ReadToEnd().Trim();
    
    Have fun
     
  13. koolmelee

    koolmelee Newbie

    Joined:
    Aug 11, 2009
    Messages:
    4
    Likes Received:
    0
    Sorry, didn't see the post above. The code I posted was practically identical =]
     
    Last edited: Aug 16, 2009
  14. koolmelee

    koolmelee Newbie

    Joined:
    Aug 11, 2009
    Messages:
    4
    Likes Received:
    0
    Sorry for double post, was lagging
     
    Last edited: Aug 16, 2009
  15. gonzo

    gonzo Newbie

    Joined:
    May 27, 2009
    Messages:
    10
    Likes Received:
    1
    Thanks for the code, Ive been looking at various C# web browser controls but so far none seem to fit the bill....
     
  16. divinci

    divinci Junior Member

    Joined:
    Sep 25, 2007
    Messages:
    111
    Likes Received:
    15