[英]java- require basic examples showing use of new HTTP Client (v4.x)
I am working to change the HTTP client in an application from Commons HTTP Client v3.x to the new HTTP Client v4.x. 我正在努力将应用程序中的HTTP客户端从Commons HTTP Client v3.x更改为新的HTTP Client v4.x. I searched but could not find good examples for the following scenarios-- can you point me to a good tutorial/article on the new HTTP Client (something similar to the excellent Community Wiki article at SO on java.net.url)? 我搜索过但找不到以下场景的好例子 - 你能指点我一篇关于新HTTP客户端的好教程/文章(类似于java.net.url上SO的优秀社区维基文章)吗?
(1) set Cookie Policy (1)设置Cookie策略
(2) set Http Proxy- defining host/domain as well as username/password (2)设置Http代理 - 定义主机/域以及用户名/密码
Currently this is done in the following manner-- 目前这是以下列方式完成的 -
Credentials credentials =
( host == null || domain == null || "".equals(host.trim()) ||
"".equals(domain.trim()) ) ?
new UsernamePasswordCredentials(username, password) :
new NTCredentials(username, password, host, domain);
client.getState().setProxyCredentials( AuthScope.ANY, credentials);
(3) Auth credentials are defined in the old http client with the following code-- (3)Auth凭证在旧的http客户端中使用以下代码定义 -
client.getState().setCredentials(
new AuthScope(urlObj.getHost(), urlObj.getPort()),
new UsernamePasswordCredentials(username, password)
);
What is the way to do this in the new HTTP client? 在新的HTTP客户端中执行此操作的方法是什么?
(4) Declaring a new HTTP Method variable and for this variable, Specifying method- as GET or POST (4)声明一个新的HTTP Method变量,对于这个变量,指定方法 - 作为GET或POST
Code used for the above currently-- 目前用于上述代码 -
HttpMethodBase method;
method = createPostMethod(url, params, multipart, charset);
method = createGetMethod(url, params, charset);
(5) Adding request headers to a method - (5)向方法添加请求标头 -
For example, to set the user agent as Default user agent, the following code is used-- 例如,要将用户代理设置为默认用户代理,请使用以下代码 -
method.addRequestHeader(new Header("User-Agent", DEFAULT_USER_AGENT));
(1)cookie thing see this from another question (1)cookie的东西从另一个问题看到这个
(2)Proxy: (2)代理:
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("username", "password"));
HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet httpget = new HttpGet("/");
from: 从:
httpcomponents-client-4.1.3\\examples\\org\\apache\\http\\examples\\client\\ClientProxyAuthentication.java httpcomponents-客户4.1.3 \\例子\\组织\\阿帕奇\\ HTTP \\例子\\客户\\ ClientProxyAuthentication.java
(3)Not sure (3)不确定
(4)Method (4)方法
HttpGet httpget = new HttpGet(url);
HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
"org=self_registered_users&" +
"goto=/portal/dt&" +
"gotoOnFail=/portal/dt?error=true");
From ClientFormLogin.java of example 来自ClientFormLogin.java的例子
(5)header field: (5)标题字段:
HttpGet get = new HttpGet(url);
get.setHeader("Content-Type", "text/html");
get.setHeader("User-Agent","Mozilla/4.0 (MobilePhone SCP-5500/US/1.0) NetFront/3.0 MMP/2.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
get.getParams().setParameter("http.socket.timeout",20000);
How about that? 那个怎么样? Just see the examples. 只看示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.