tags:

views:

98

answers:

3

I am new in python. I wanna build a bot in c#. Can i use this "urllib2" in dot net or is there any alternative in dot net? please help...

+1  A: 

Consider using HttpRequest and/or WebClient. Or, you may need to use sockets. It depends on what kind of bot you want to build.

Also, there is a python implementation for .NET called IronPython. This is able to use standard python libraries and the .NET framework as well.


On a side note I'd recommend you to choose the right language/framework after you found out what you want to do and observed the alternatives, not before that.

Tamás Szelei
A: 

http://curl.haxx.se/libcurl/dotnet/ could help also.

Blindy
+1  A: 

Most of the equivalent functionality is in System.Web namespace:

The System.Web namespace supplies classes and interfaces that enable browser-server communication. This namespace includes the HttpRequest class, which provides extensive information about the current HTTP request; the HttpResponse class, which manages HTTP output to the client; and the HttpServerUtility class, which provides access to server-side utilities and processes. System.Web also includes classes for cookie manipulation, file transfer, exception information, and output cache control.

A close cousin of urlopen is the System.Net.Webclient class:

Provides common methods for sending data to and receiving data from a resource identified by a URI.

using System;
using System.Net;
using System.IO;

public class Test
{
    public static void Main (string[] args)
    {
        if (args == null || args.Length == 0)
        {
            throw new ApplicationException ("Specify the URI of the resource to retrieve.");
        }
        WebClient client = new WebClient ();

        // Add a user agent header in case the 
        // requested URI contains a query.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead (args[0]);
        StreamReader reader = new StreamReader (data);
        string s = reader.ReadToEnd ();
        Console.WriteLine (s);
        data.Close ();
        reader.Close ();
    }
}
gimel
@gimel , i already use this code in various parts of my projects. thanks for this . But i need the urllib2.build_opener([handler, ...]) functionality of the following page. http://docs.python.org/library/urllib2.html Can you provide me any clue how to use this in dot net?
Sorry, no multi-handler model in HttpWebRequest. Some properties provide similar functions, e.g. http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.proxy.aspx
gimel
@Tareq: Why didn't you ask that in the first place?
Tamás Szelei