先看接口抓包详情:
java代码发送这个请求的代码如下:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
package xia.wenjie;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
/**
* <br>Classname: HttpClientDemo
* <br>描述:
* <br>功能:
* <br>作者:
* <br>时间: 2021/1/23 15:48
*/
public class HttpClientDemo {
public static void main(String[] args) throws ParseException, IOException {
//建立NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("grant_type", "password"));
nvps.add(new BasicNameValuePair("username", "xxxxxxx"));
nvps.add(new BasicNameValuePair("password", "xxxxxxx"));
// nvps.add(new BasicNameValuePair("", ""));
// nvps.add(new BasicNameValuePair("", ""));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, Consts.UTF_8);
//初始化httpClient
HttpClient httpClient = HttpClients.custom().build();
//使用的请求方法
HttpPost httpPost = new HttpPost("http://xxxxxxxxxx/api-uaa/password/token");
httpPost.setEntity(entity);
//请求头配置
httpPost.setHeader("Authorization", "Basic xxxxx");
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
// httpPost.setHeader("Content-Length", "481");
httpPost.setHeader("Host", "xxxxx.com");
httpPost.setHeader("Accept-Encoding", "gzip");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("User-Agent", "PostmanRuntime/7.26.8");
// 头部除了Authorization是必要的,其他的可以不要也可以请求成功,抓包有的建议加上
HttpResponse response = null;
try {
//表单参数提交
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.abort(); //释放连接
}
System.out.println(response.getStatusLine());
String entityString = EntityUtils.toString(response.getEntity(), "gbk"); //注意设置编码
System.out.println(entityString);
}
}
jmeter发送请求:
jmeter 使用HTTP Authorization Manager
jmeter 应该可以不使用授权管理器,直接像java 代码设置头部
httpPost.setHeader(“Authorization”, “Basic xxxxx”);
测试一下也是可行的。
补充:
httpPost.setHeader(“Authorization”, “Basic xxxxx”); Basic xxxxx的值可以这样生成再设置进去也是有效的:
// Username:Password= afxx:afxx YWZsYzphxxx
String auth = "afxx" + ":" + "afxx";
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
System.out.println(authHeader);
看阿帕奇的官网:
http://hc.apache.org/httpcomponents-client-4.5.x/examples.html
发现上面的java代码,可以不用设置头部的所有信息也是可以的:
package xia.wenjie;
/**
* <br>Classname: ClientPreemptiveBasicAuthentication
* <br>描述:
* <br>功能:
* <br>作者: xiawenjie
* <br>时间: 2021/1/25 10:03
*/
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
/**
* An example of HttpClient can be customized to authenticate
* preemptively using BASIC scheme.
* <b>
* Generally, preemptive authentication can be considered less
* secure than a response to an authentication challenge
* and therefore discouraged.
*/
public class ClientPreemptiveBasicAuthentication {
// http://testxxx.xxxx.com/api-uaa/password/token
public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("testxxx.xxxxx.com", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("afxx", "afxx"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpPost httppost= new HttpPost("http://testxxx.xxxx.com/api-uaa/password/token");
//建立NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("grant_type", "passwxxxx"));
nvps.add(new BasicNameValuePair("username", "1311955xxxx|xxx1"));
nvps.add(new BasicNameValuePair("password", "29ad0e3fd3db681fb9f8091c7xxxx"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, Consts.UTF_8);
httppost.setEntity(entity);
System.out.println("Executing request " + httppost.getRequestLine() + " to target " + target);
for (int i = 0; i < 3; i++) {
CloseableHttpResponse response = httpclient.execute(target, httppost, localContext);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}
}
} finally {
httpclient.close();
}
}
}
CloseableHttpResponse response = httpclient.execute(target, httppost, localContext);
用 CloseableHttpResponse response = httpclient.execute(httppost, localContext); 也可以
HTTP身份认证可参考:
https://blog.csdn.net/ktlifeng/article/details/51099370