一、pom依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.3</version>
</dependency>
二、调用接口,塞入header和body
public Request bodyString(final String s, final ContentType contentType) 指定body的格式,body内容需要是string
public void delete(){
JSONObject content = new JSONObject();
content.put("mode","delete");
content.put("name","jiagou_mqtest.rabbitTest");
content.put("vhost","/" );
try {
Request.Post(newUrl).bodyString(content.toString(), ContentType.APPLICATION_JSON).addHeader("authorization","Basic cmFiYml0bXE6cmFiYml0bXE=")
.execute().returnResponse().getStatusLine().getStatusCode();//获取返回的状态码
}catch(Exception e){
e.printStackTrace();
}
}
body是表单(可参考http://blog.csdn.net/vector_yi/article/details/24298629)
List forms = Form.form()
.add("para1", para1)
.add("para2", para2)
.build();
Request.Post(uri).bodyForm(forms).execute().returnResponse().getStatusLine().getStatusCode();
Request.Post("http://blog.csdn.net/vector_yi")
.addHeader("X-Custom-header", "stuff")
.viaProxy(new HttpHost("myproxy", 8080))
.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
.execute().saveContent(new File("result.dump"));