【问题标题】:What is the python's "urllib2" equivalent in dot net?dot net 中 python 的“urllib2”等价物是什么?
【发布时间】:2010-11-15 17:59:05
【问题描述】:

我是 python 新手。我想用 C# 构建一个机器人。我可以在 dot net 中使用这个“urllib2”还是在 dot net 中有任何替代方法?请帮忙...

【问题讨论】:

    标签: c# bots


    【解决方案1】:

    考虑使用HttpRequest 和/或WebClient。或者,您可能需要使用sockets。这取决于您要构建什么样的机器人。

    此外,还有一个用于 .NET 的 Python 实现,名为 IronPython。这也可以使用标准的 Python 库和 .NET 框架。


    顺便说一句,我建议您选择正确的语言/框架在您发现自己想做的事情并观察替代方案之后,而不是在此之前。

    【讨论】:

      【解决方案2】:

      http://curl.haxx.se/libcurl/dotnet/ 也可以提供帮助。

      【讨论】:

        【解决方案3】:

        大部分等效功能位于System.Web 命名空间中:

        System.Web 命名空间提供支持浏览器-服务器通信的类和接口。这个命名空间包括 HttpRequest 类,它提供了有关当前 HTTP 请求的大量信息; HttpResponse 类,它管理到客户端的 HTTP 输出;和 HttpServerUtility 类,它提供对服务器端实用程序和进程的访问。 System.Web 还包括用于 cookie 操作、文件传输、异常信息和输出缓存控制的类。

        urlopen 的近亲是 System.Net.Webclient 类:

        提供向由 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 ,我已经在我的项目的各个部分使用了这段代码。谢谢你。但我需要下一页的 urllib2.build_opener([handler, ...]) 功能。 docs.python.org/library/urllib2.html你能告诉我如何在dot net中使用它吗?
        • 抱歉,HttpWebRequest 中没有多处理器模型。一些属性提供类似的功能,例如msdn.microsoft.com/en-us/library/…
        • @Tareq:你为什么不一开始就问这个?
        猜你喜欢
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        • 2014-06-29
        • 2011-10-13
        • 2011-03-23
        相关资源
        最近更新 更多