gpt4 book ai didi

c# - python 的 "urllib2"在 dot net 中的等价物是什么?

转载 作者:行者123 更新时间:2023-11-30 14:43:58 24 4
gpt4 key购买 nike

我是 python 新手。我想用 C# 构建一个机器人。我可以在 dot net 中使用这个“urllib2”还是在 dot net 中有其他选择?请帮助...

最佳答案

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

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.

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

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 ();
}
}

关于c# - python 的 "urllib2"在 dot net 中的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1211674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com