简体   繁体   English

java-需要显示使用新HTTP客户端的基本示例(v4.x)

[英]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.

相关问题 Java-apache http客户端使用示例,显示使用cookie和从HTTPResponse对象中提取响应 - Java- apache http client- usage examples showing use of cookies and extracting response from HTTPResponse object java-使用Apache HTTP客户端时未知的主机异常 - java- unknown host exception when using apache http client Java-JTable没有显示网格线 - Java- JTable not showing gridlines java-apache http客户端查询,涉及作为多部分发布请求的一部分提交文件 - java- apache http client-query regarding submission of files as part of a multi part post request java - RejectedExecutionException 中的 amazon dynamo db 客户端 - amazon dynamo db client in java- RejectedExecutionException Java 11:新的 HTTP 客户端发送带有 x-www-form-urlencoded 参数的 POST 请求 - Java 11: New HTTP client send POST requests with x-www-form-urlencoded parameters Java-在GUI中向JTable添加新行 - Java- adding a new row to a JTable in GUI Java-用新方法实现伯努利数 - Java- Implementation of Bernoulli Numbers in new method 使用Java和async-http-client进行基本身份验证的URL内容 - Get URL content with Basic Authentication with Java and async-http-client java-如何使用服务发送电子邮件? - java- How to use services for sending email?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM