Equivalent of java.net.URLConnection in .NET

家住魔仙堡 提交于 2019-12-24 15:56:48

问题


Is there an equivalent of the java.net.URLConnection class in .NET. , for example the HttpWebRequest? What else could be used?


回答1:


Probably the closest is:

WebRequest req = WebRequest.Create(url); // note this is IDisposable so
                                         // should be in a "using" block, or
                                         // otherwise disposed.

since this will handle multiple protocols etc. But if you are meaning http - I'd use WebClient; it is much simpler than HttpWebRequest (one of the WebRequest implementations).

If all you want is to download a page:

string s;
using(var client = new WebClient()) {
    s = client.DownloadString(url);
}



回答2:


HttpWebRequest and WebClient are as close as I can see.

Is there a specific feature or set of features that you require?



来源:https://stackoverflow.com/questions/4345340/equivalent-of-java-net-urlconnection-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!