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

我的未来我决定 提交于 2019-12-18 04:46:12

问题


.NET has the HttpWebRequest and WebClient classes for simulating a browser's requests.

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 .jar and gives me back the response in a text string or some other parseable structure.


回答1:


HttpURLConnection is Java's equivalent of HttpWebRequest.

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



回答2:


Apache HTTPClient has equivalent functionality, though the APIs are not exactly the same. Oakland Software has a table comparing 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:

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

Here's a link to the HTTPClient tutorial.




回答3:


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




回答4:


Verify Webclient in Apache Cx JaxRs Library.

Checkout this: 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);


来源:https://stackoverflow.com/questions/1137812/equivalent-of-nets-webclient-and-httpwebrequest-in-java

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