Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

.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.

like image 871
MatthewMartin Avatar asked Jul 16 '09 14:07

MatthewMartin


People also ask

Is HttpWebRequest obsolete?

NET 6, the WebRequest, WebClient, and ServicePoint classes are deprecated. The classes are still available, but they're not recommended for new development.

Which is better HttpClient or WebClient?

WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with . NET Framework 4.5.

What is HttpWebRequest?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

What is Httpwebresponse C#?

GetResponseStream Method (System.Net) Gets the stream that is used to read the body of the response from the server.


2 Answers

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);
like image 109
Vallabha Vamaravelli Avatar answered Oct 19 '22 23:10

Vallabha Vamaravelli


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.

like image 20
Vinay Sajip Avatar answered Oct 19 '22 23:10

Vinay Sajip