我应该使用哪些对象进行HttpClient编程?

时间:2022-01-16 07:07:09

I'm trying to write a class that will be able to send requests to servers through URLs and receive responses from the server as well.

我正在尝试编写一个类,它将能够通过URL向服务器发送请求,并从服务器接收响应。

I've been researching HttpClient but it seems like there are a lot of different types of HttpClient objects from various users. I'm not sure which I should use. I'd prefer to use Apache objects if at all possible.

我一直在研究HttpClient,但似乎各种用户都有很多不同类型的HttpClient对象。我不确定我应该使用哪个。如果可能的话,我更愿意使用Apache对象。

Requirements:

Must be able to send requests through urls using the get method. I'd like to use HttpMethod for this. Must be able to submit a form to either the get or post method. Must be able to get and store cookies from a response as well as send those cookies in a request header. The ability to spoof the referrer header would be useful as well.

必须能够使用get方法通过URL发送请求。我想为此使用HttpMethod。必须能够将表单提交给get或post方法。必须能够从响应中获取和存储Cookie,并在请求标头中发送这些Cookie。欺骗引用者头部的能力也是有用的。

Some example code of what I'm trying to do:

我正在尝试做的一些示例代码:

String searchURL = //however I'm generating the search URL
String responeAsString = "";
HttpClient client = new HttpClient(new MultiThreadedConnectionManager());
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
HttpMethod method = new GetMethod(searchURL);

try{
    client.executeMethod(method);
    responseAsString = method.getResponseBodyAsString();
}catch (Exception e){
    //error fixing code
}

I'd like to be able to replace responseAsString with an actual HttpResponse object, but I haven't been able to find a resource of where to download a jar file.

我希望能够用实际的HttpResponse对象替换responseAsString,但是我无法找到下载jar文件的资源。

If you could include links on where to download the .jar files of the objects you suggest, that would be great.

如果你可以包含下载你建议的对象的.jar文件的链接的链接,那将是很棒的。

Thank you!

3 个解决方案

#1


0  

This might be a little overkill but I'd look at the open source project GWT https://developers.google.com/web-toolkit/

这可能有点矫枉过正,但我​​会看一下开源项目GWT https://developers.google.com/web-toolkit/

They have a sample application which does communicate with the server and you could see how it works.

他们有一个示例应用程序,它与服务器进行通信,您可以看到它是如何工作的。

Good luck

#2


0  

You can find a Java HTTP Client comparison at http://oaklandsoftware.com/product_http/compare.html

您可以在http://oaklandsoftware.com/product_http/compare.html找到Java HTTP Client比较

Your requisites seem basic so they should be supported by any library, except for the last one about the HttpResponse object but using Apache library you can get the body, headers and footers of the response from HttpMethod, this sould be enough. Other libraries should offer something similar, I suppose.

你的必需品似乎是基本的,所以它们应该得到任何库的支持,除了关于HttpResponse对象的最后一个,但是使用Apache库你可以得到来自HttpMethod的响应的正文,页眉和页脚,这就足够了。其他图书馆应该提供类似的东西,我想。

#3


0  

You can do everything u requested with the Apache Default Http Client version 4 you can modify the response string to any valid HTTPResponse object. you can change all the headers in the request and response, including the referrer header. you should only know the header name to be able to replace it.

您可以使用Apache Default Http Client版本4执行所有请求,您可以将响应字符串修改为任何有效的HTTPResponse对象。您可以更改请求和响应中的所有标头,包括引用标头。您应该只知道标题名称才能替换它。

The library is available at http://hc.apache.org/httpcomponents-client-ga/

该库可从http://hc.apache.org/httpcomponents-client-ga/获得。

please make sure you get the version 4.

请确保您获得版本4。

A guide on how to use it is available at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/

有关如何使用它的指南,请访问:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/

you can find the list of request and response headers you can use at: first link given by google search for keyword "http response headers" - the wiki article.

你可以找到你可以使用的请求和响应标题列表:谷歌搜索关键字“http响应标题” - 维基文章给出的第一个链接。

Enjoy!!

#1


0  

This might be a little overkill but I'd look at the open source project GWT https://developers.google.com/web-toolkit/

这可能有点矫枉过正,但我​​会看一下开源项目GWT https://developers.google.com/web-toolkit/

They have a sample application which does communicate with the server and you could see how it works.

他们有一个示例应用程序,它与服务器进行通信,您可以看到它是如何工作的。

Good luck

#2


0  

You can find a Java HTTP Client comparison at http://oaklandsoftware.com/product_http/compare.html

您可以在http://oaklandsoftware.com/product_http/compare.html找到Java HTTP Client比较

Your requisites seem basic so they should be supported by any library, except for the last one about the HttpResponse object but using Apache library you can get the body, headers and footers of the response from HttpMethod, this sould be enough. Other libraries should offer something similar, I suppose.

你的必需品似乎是基本的,所以它们应该得到任何库的支持,除了关于HttpResponse对象的最后一个,但是使用Apache库你可以得到来自HttpMethod的响应的正文,页眉和页脚,这就足够了。其他图书馆应该提供类似的东西,我想。

#3


0  

You can do everything u requested with the Apache Default Http Client version 4 you can modify the response string to any valid HTTPResponse object. you can change all the headers in the request and response, including the referrer header. you should only know the header name to be able to replace it.

您可以使用Apache Default Http Client版本4执行所有请求,您可以将响应字符串修改为任何有效的HTTPResponse对象。您可以更改请求和响应中的所有标头,包括引用标头。您应该只知道标题名称才能替换它。

The library is available at http://hc.apache.org/httpcomponents-client-ga/

该库可从http://hc.apache.org/httpcomponents-client-ga/获得。

please make sure you get the version 4.

请确保您获得版本4。

A guide on how to use it is available at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/

有关如何使用它的指南,请访问:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/

you can find the list of request and response headers you can use at: first link given by google search for keyword "http response headers" - the wiki article.

你可以找到你可以使用的请求和响应标题列表:谷歌搜索关键字“http响应标题” - 维基文章给出的第一个链接。

Enjoy!!