Java 获取带有参数和身份验证的请求
Java Get request with parameter and Authetification
我想在我的请求中加入一些参数(代码值和名称值):
String code = "001";
String name = "AAA";
HttpGet request = new HttpGet(url);
String auth = user + ":" + mdp;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
如何使用身份验证请求执行此操作?
类似于:code=001&name=AAA
在 URL
使用 URIBuilder
构建您的 URL
import org.apache.http.client.utils.URIBuilder;
//...
final String URI_PARAM_CODE = "code";
final String URI_PARAM_NAME = "name";
String code = "001";
String name = "AAA";
URI uri = new URIBuilder("http://google.com")
.setParameter(URI_PARAM_CODE, code)
.setParameter(URI_PARAM_NAME, name)
.build();
您还可以在调用之前设置其他有用的属性URIBuilder::build
- 请参考Apache URIBuilder documentation。
我想在我的请求中加入一些参数(代码值和名称值):
String code = "001";
String name = "AAA";
HttpGet request = new HttpGet(url);
String auth = user + ":" + mdp;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
如何使用身份验证请求执行此操作?
类似于:code=001&name=AAA
在 URL
使用 URIBuilder
构建您的 URL
import org.apache.http.client.utils.URIBuilder;
//...
final String URI_PARAM_CODE = "code";
final String URI_PARAM_NAME = "name";
String code = "001";
String name = "AAA";
URI uri = new URIBuilder("http://google.com")
.setParameter(URI_PARAM_CODE, code)
.setParameter(URI_PARAM_NAME, name)
.build();
您还可以在调用之前设置其他有用的属性URIBuilder::build
- 请参考Apache URIBuilder documentation。