httpclient ajax post,httpclient4.4 get、post(包括文件传送方法记录)

本文介绍了如何使用Apache HttpClient库进行HTTP POST表单提交和文件上传,包括创建HttpClient实例、构造HttpPost请求、设置参数、执行请求并处理响应。重点展示了post方法和multipart/form-data格式的文件上传过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven依赖:

org.apache.httpcomponents

httpclient

4.4

org.apache.httpcomponents

httpmime

4.4

​​

package com.xinyuan.haze.common.utils;

import java.io.File;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import

org.apache.http.client.ClientProtocolException;

import

org.apache.http.client.entity.UrlEncodedFormEntity;

import

org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;

import

org.apache.http.entity.mime.MultipartEntityBuilder;

import

org.apache.http.entity.mime.content.FileBody;

import

org.apache.http.entity.mime.content.StringBody;

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;

public class HttpClientUtils {

//post提交表单

public static String post(String url, Map params)

{

String body = null;

// 创建默认的httpClient实例.

CloseableHttpClient httpclient =

HttpClients.createDefault();

// 创建httppost

HttpPost httppost = new

HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");

// 创建参数队列

List formparams = new ArrayList();

formparams.add(new BasicNameValuePair("type",

"house"));

UrlEncodedFormEntity uefEntity;

try {

uefEntity

= new UrlEncodedFormEntity(formparams, "UTF-8");

httppost.setEntity(uefEntity);

System.out.println("executing request " + httppost.getURI());

CloseableHttpResponse response = httpclient.execute(httppost);

try {

HttpEntity entity =

response.getEntity();

if (entity != null) {

System.out.println("--------------------------------------");

System.out.println("Response content: " +

EntityUtils.toString(entity, "UTF-8"));

System.out.println("--------------------------------------");

}

} finally

{

response.close();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

//

关闭连接,释放资源

try {

httpclient.close();

} catch

(IOException e) {

e.printStackTrace();

}

}

return body;

}

//给请求

public static String

get(String url) {

String body =

null;

CloseableHttpClient

httpclient =

HttpClients.createDefault();

CloseableHttpResponse response = null;

try {

// 创建httpget.

HttpGet

httpget = new HttpGet(url);

System.out.println("executing request " +

httpget.getURI());

// 执行get请求.

response = httpclient.execute(httpget);

// 获取响应实体

HttpEntity

entity = response.getEntity();

System.out.println("--------------------------------------");

// 打印响应状态

System.out.println(response.getStatusLine());

if (entity

!= null) {

// 打印响应内容长度

System.out.println("Response

content length: " + entity.getContentLength());

// 打印响应内容

System.out.println("Response

content: " + EntityUtils.toString(entity));

}

System.out.println("------------------------------------");

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {

//

关闭连接,释放资源

try {

httpclient.close();

} catch

(IOException e) {

e.printStackTrace();

}

if(response != null){

try

{

response.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return body;

}

public void upload()

{

CloseableHttpClient httpclient =

HttpClients.createDefault();

try {

HttpPost

httppost = new

HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");

FileBody

bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));

StringBody

comment = new StringBody("A binary file of some kind",

ContentType.TEXT_PLAIN);

HttpEntity

reqEntity = MultipartEntityBuilder.create().addPart("bin",

bin).addPart("comment", comment).build();

httppost.setEntity(reqEntity);

System.out.println("executing request " +

httppost.getRequestLine());

CloseableHttpResponse response = httpclient.execute(httppost);

try {

System.out.println("----------------------------------------");

System.out.println(response.getStatusLine());

HttpEntity resEntity =

response.getEntity();

if (resEntity != null) {

System.out.println("Response content length: " +

resEntity.getContentLength());

}

EntityUtils.consume(resEntity);

} finally

{

response.close();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

httpclient.close();

} catch

(IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值