ResteasyClient 怎么设置请求的编码格式

 我来答
huanglenzhi
2016-02-06 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
package com.supermap.earth.rims.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class CustomerHttpClient {
private static final String TAG = "CustomerHttpClient";
private static final String ENCODING = HTTP.UTF_8;
private static HttpClient customerHttpClient;

private CustomerHttpClient() {
}

public static synchronized HttpClient getHttpClient() {
if (null == customerHttpClient) {
HttpParams params = new BasicHttpParams();
// 设置一些基本参数
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, ENCODING);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams
.setUserAgent(
params,
"Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
ConnManagerParams.setTimeout(params, 5000);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 4000);

SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));

ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}

public static String post(String url, NameValuePair... params) {
try {
// 编码参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); // 请求参数
for (NameValuePair p : params) {
formparams.add(p);
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
ENCODING);

// 创建POST请求
HttpPost request = new HttpPost(url);
request.setEntity(entity);
// 发送请求
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new RuntimeException("请求失败");
}
HttpEntity resEntity = response.getEntity();
return (resEntity == null) ? null : EntityUtils.toString(resEntity,
ENCODING);
} catch (UnsupportedEncodingException e) {
return null;
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
throw new RuntimeException("连接失败", e);
}

}

/**
* httpclient不直接支持及mime multipart方式上传附件,需要引入第三方类库
* D:\Downloads\android\apache mime\apache-mime4j-0.6.jar
* D:\Downloads\android\apache mime\commons-io-2.1.jar
* D:\Downloads\android\apache mime\httpmime-4.0.jar
*/
public static boolean httpPostUpload(String serverUrl, String headContent,
String fileName) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl);

// multipart实体
MultipartEntity entity = new MultipartEntity();
entity.addPart("picMsg", new StringBody(headContent));
entity.addPart("pic", new FileBody(new File(fileName)));
post.setEntity(entity);
HttpResponse resp = client.execute(post);
if (resp.getStatusLine().getStatusCode() == 200) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// ////////////////////////////////////////////////////////////////

/**
* 获取图片流
*
* @param uri
* 图片地址
*
* @return
* @throws MalformedURLException
*/
public static InputStream GetImageByUrl(String uri)
throws MalformedURLException {
URL url = new URL(uri);
URLConnection conn;
InputStream is;
try {
conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
return is;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

public static void saveFile(InputStream fis, String saveDir)
throws Exception {

FileOutputStream fos = new FileOutputStream(new File(saveDir));
byte[] b = new byte[1];
while (fis.read(b) != -1) {
fos.write(b);
fos.flush();
}
fis.close();
fos.close();
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式