58 lines
1.9 KiB
Markdown
58 lines
1.9 KiB
Markdown
## 依赖引入
|
||
|
||
``` xml
|
||
<dependency>
|
||
<groupId>org.apache.httpcomponents</groupId>
|
||
<artifactId>httpclient</artifactId>
|
||
<version>4.5.3</version>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
## 请求示例代码
|
||
|
||
```java
|
||
public void imitateCookie() throws UnsupportedEncodingException {
|
||
CloseableHttpClient httpclient= HttpClients.createDefault();
|
||
String url = "http://hpc.kaiyuancloud.cn/rms/api";
|
||
// 开元云
|
||
String cipher = "WIfHaMnen2WaRw4Agwenoe6TwDD3LxBbLEgMjVKDHADPE7xDJicjSG3yAfK4iNes1vUmVcoTxV2/+gMfla8ZDg==";
|
||
|
||
HttpPost postMethod = new HttpPost(url + "/sign");
|
||
List<NameValuePair> params=new ArrayList<NameValuePair>();
|
||
//建立一个NameValuePair数组,用于存储欲传送的参数
|
||
params.add(new BasicNameValuePair("cipher", cipher));
|
||
//添加参数
|
||
postMethod.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
|
||
HttpResponse response;
|
||
try {
|
||
response = httpclient.execute(postMethod);
|
||
System.out.println(EntityUtils.toString(response.getEntity()));
|
||
|
||
Header[] headers= response.getAllHeaders();
|
||
String cookie=null;
|
||
for(Header header:headers){
|
||
if(header.getName().equals("Set-Cookie"))
|
||
cookie=header.getValue();
|
||
}
|
||
|
||
if(cookie!=null){
|
||
|
||
HttpGet getMethodWithCookie = new HttpGet(url + "/biz/cluster/list");
|
||
getMethodWithCookie.addHeader("Cookie",cookie);
|
||
response=httpclient.execute(getMethodWithCookie);
|
||
System.out.println(EntityUtils.toString(response.getEntity()));
|
||
}
|
||
|
||
} catch (ClientProtocolException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
} catch (IOException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
```
|
||
|