相当于 Java 中的 .NET 的 WebClient 和 HttpWebRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:10:00  来源:igfitidea点击:

Equivalent of .NET's WebClient and HttpWebRequest in Java?

java.netlanguage-comparisons

提问by MatthewMartin

.NET has the HttpWebRequestand WebClientclasses for simulating a browser's requests.

.NET 具有用于模拟浏览器请求的HttpWebRequestWebClient类。

I'd google it, but I'm not sure what keyword to use.

我会谷歌它,但我不确定使用什么关键字。

I want to write code that does does HTTP GETs and POSTs, along with cookies, in an applet or local .jarand gives me back the response in a text string or some other parseable structure.

我想编写在小程序或本地执行 HTTP GET 和 POST 以及 cookie 的代码,.jar并将响应以文本字符串或其他一些可解析的结构形式返回给我。

采纳答案by Mitch Wheat

HttpURLConnectionis Java's equivalent of HttpWebRequest.

HttpURLConnection是 Java 的等价物HttpWebRequest

URL iurl = new URL(url);
HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();
uc.connect();
if (uc.getContentType().equalsIgnoreCase("image/jpeg"))
{
  result = true;
}

回答by Vinay Sajip

Apache HTTPClienthas equivalent functionality, though the APIs are not exactly the same. Oakland Software has a tablecomparing their commercial product with various alternatives, including the Apache product. Apache's own opinion of the built-in HttpUrlConnection (quoted from the above linked-to page) is:

Apache HTTPClient具有等效的功能,但 API 并不完全相同。Oakland Software 有一张表格,将他们的商业产品与各种替代品(包括 Apache 产品)进行了比较。Apache 自己对内置 HttpUrlConnection(引自上述链接页面)的看法是:

The jdk has the HttpUrlConnection which is limited and in many ways flawed.

jdk 的 HttpUrlConnection 是有限的,并且在很多方面都有缺陷。

Here's a link to the HTTPClient tutorial.

这是 HTTPClient教程的链接。

回答by Titi Wangsa bin Damhore

html unit for me. i can simulate javascript (to a certain extent)

html 单元给我。我可以模拟 javascript(在一定程度上)

回答by Vallabha Vamaravelli

Verify Webclient in Apache Cx JaxRs Library.

在 Apache Cx JaxRs 库中验证 Webclient。

Checkout this: https://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/client/WebClient.html

签出这个:https: //cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/client/WebClient.html

Sample code looks below:

示例代码如下:

WebClient client = WebClient.create(url);
client.path(ADD_PATH).path("/books/2").accept("text/plain");
s = client.get(String.class);
System.out.println(s);