使用 HttpClient4.3版本进行微信开发。使用了httpclient的get请求,以及上传文件。
import java.io.File;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
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.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 对应微信公众平台API的:基础支持
*
* @author zyk
*
*/
@SuppressWarnings("deprecation")
public class BaseSupport {
/**
* 获取微信access_token
*
* @param apiurl
* 微信APIurl
* @param appid
* 微信appid
* @param secret
* 微信secret
* @return access_token字符串
* @throws IOException
*/
public static String GetAccesstoken(String apiurl, String appid,
String secret) throws IOException {
String turl = String.format(
"%s?grant_type=client_credential&appid=%s&secret=%s", apiurl.trim(),
appid.trim(), secret.trim());
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(turl);
String access_token = null;
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
String entityString = EntityUtils.toString(entity);
JSONObject fromObject = JSONObject.fromObject(entityString);
access_token = fromObject.get("access_token").toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(httpResponse != null){
httpResponse.close();
}
if(httpClient!= null){
httpClient.close();
}
}
return access_token;
}
/**
* 上传多媒体文件
* @param access_token
* @param type 图片(image)、语音(voice)、视频(video)和缩略图(thumb)
* 图片(image): 1M,支持JPG格式
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3\SPEEX格式
* 视频(video):10MB,支持MP4格式
* 缩略图(thumb):64KB,支持JPG格式
* @param fileUrl
* @return
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static String uploadMedia(String access_token, String type,
String fileUrl) throws IOException {
String media_id = null;
CloseableHttpResponse response = null;
String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="
+ access_token + "&type=" + type;
CloseableHttpClient httpclient = null;
httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
FileBody bin = new FileBody(new File(fileUrl));
MultipartEntity mpEntity = new MultipartEntity(); // 文件传输
mpEntity.addPart("media", bin);
httpPost.setEntity(mpEntity);
try {
response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity);
JSONObject fromObject = JSONObject.fromObject(jsonString);
Object media_idObject = fromObject.get("media_id");
if (media_idObject != null) {
media_id = media_idObject.toString();
} else {
System.out.println(fromObject.toString());
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(response != null){
response.close();
}
if(httpclient!= null){
httpclient.close();
}
}
return media_id;
}
}
MultipartEntity 已经被废弃,暂时没有找到新的。