本文整理匯總了Java中org.apache.http.entity.StringEntity.setContentEncoding方法的典型用法代碼示例。如果您正苦於以下問題:Java StringEntity.setContentEncoding方法的具體用法?Java StringEntity.setContentEncoding怎麽用?Java StringEntity.setContentEncoding使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.entity.StringEntity
的用法示例。
在下文中一共展示了StringEntity.setContentEncoding方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doPost
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
public static String doPost(String url, String json) throws Exception {
try {
CloseableHttpClient client = getHttpClient(url);
HttpPost post = new HttpPost(url);
config(post);
logger.info("====> Executing request: " + post.getRequestLine());
if (!StringUtils.isEmpty(json)) {
StringEntity s = new StringEntity(json, "UTF-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
}
String responseBody = client.execute(post, getStringResponseHandler());
logger.info("====> Getting response from request " + post.getRequestLine() + " The responseBody: " + responseBody);
return responseBody;
} catch (Exception e) {
if (e instanceof HttpHostConnectException || e.getCause() instanceof ConnectException) {
throw new ConnectException("====> 連接服務器" + url + "失敗: " + e.getMessage());
}
logger.error("====> HttpRequestUtil.doPost: " + e.getMessage(), e);
}
return null;
}
示例2: testService
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
@RequestMapping(value = "testService", method = RequestMethod.POST)
public Object testService(@RequestParam(value = "ipPort", required = true) String ipPort,
@RequestBody GrpcServiceTestModel model) throws Exception {
String serviceUrl = "http://" + ipPort + "/service/test";
HttpPost request = new HttpPost(serviceUrl);
request.addHeader("content-type", "application/json");
request.addHeader("Accept", "application/json");
try {
StringEntity entity = new StringEntity(gson.toJson(model), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
request.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String minitorJson = EntityUtils.toString(httpResponse.getEntity());
Object response = gson.fromJson(minitorJson, new TypeToken<Object>() {}.getType());
return response;
}
} catch (Exception e) {
throw e;
}
return null;
}
示例3: testPost
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
@Test
public void testPost() throws IOException {
String ip = "冰箱冰箱冰箱冰箱冰箱冰箱冰箱";
// 創建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
// 請求參數
StringEntity entity = new StringEntity("", DEFAULT_ENCODE);
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
HttpPost httpPost = new HttpPost("https://m.fangliaoyun.com");
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
//此處區別PC終端類型
httpPost.addHeader("typeFlg", "9");
//此處增加瀏覽器端訪問IP
httpPost.addHeader("x-forwarded-for", ip);
httpPost.addHeader("Proxy-Client-IP", ip);
httpPost.addHeader("WL-Proxy-Client-IP", ip);
httpPost.addHeader("HTTP_CLIENT_IP", ip);
httpPost.addHeader("X-Real-IP", ip);
httpPost.addHeader("Host", ip);
httpPost.setEntity(entity);
httpPost.setConfig(RequestConfig.DEFAULT);
HttpResponse httpResponse;
// post請求
httpResponse = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(httpEntity.getContent());
//釋放資源
closeableHttpClient.close();
}
示例4: httpPostRequest
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
public static String httpPostRequest(String url, String params) throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
StringEntity entitystring = new StringEntity(params, "utf-8");//解決中文亂碼問題
entitystring.setContentEncoding("UTF-8");
entitystring.setContentType("application/json");
httpPost.setEntity(entitystring);
return getResult(httpPost);
}
示例5: postJson
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
/** post
* @param url 請求的url
* @param queries 請求的參數,在瀏覽器?後麵的數據,沒有可以傳null
* @param obj post obj 提交json
* @return
* @throws IOException
*/
public <T> String postJson(String url, Map<String, String> queries, T obj) throws IOException {
String responseBody = "";
CloseableHttpClient httpClient = getHttpClient();
StringBuilder sb = new StringBuilder(url);
appendQueryParams(queries, sb);
//指定url,和http方式
HttpPost httpPost = new HttpPost(sb.toString());
if (SetTimeOut) {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(SocketTimeout)
.setConnectTimeout(ConnectTimeout).build();//設置請求和傳輸超時時間
httpPost.setConfig(requestConfig);
}
//添加參數
String jsonbody = null;
if (obj != null) {
jsonbody = JSONObject.toJSONString(obj);
}
//設置參數到請求對象中
StringEntity stringEntity = new StringEntity(jsonbody,"utf-8");//解決中文亂碼問題
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
//請求數據
CloseableHttpResponse response = httpClient.execute(httpPost);
responseBody = getResponseString(responseBody, response);
return responseBody;
}
示例6: postString
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
/**
* http post string請求
*
* @param url 請求地址
* @param data 請求數據
* @param type 請求數據格式
* @return result 相應結果
*/
public static String postString(String url, String data, String type) {
CloseableHttpClient httpClient = HttpClientFactory.createHttpClient();
HttpPost request = new HttpPost(url);
StringEntity stringEntity = new StringEntity(data, "UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType(type);
request.setEntity(stringEntity);
return execute(httpClient, request);
}
示例7: postJson
import org.apache.http.entity.StringEntity; //導入方法依賴的package包/類
/**
* httppost請求(PostMethod)
*
* @param url
* @param data
* @return String
* @author liuxx
*/
public static final <T extends Object> T postJson(String url,String token, Object data, Class<T> clazz) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
if(!StringHelper.isNullOrWhiteSpace(token)){
post.setHeader("sso.token", token);
}
T result;
try {
if (data != null) {
StringEntity s = new StringEntity(JSON.toJSONString(data), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(s);
}
// 發送請求
HttpResponse httpResponse = client.execute(post);
// 獲取響應輸入流
InputStream inStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
String response;
StringBuilder sbResponse = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) sbResponse.append(line + "\n");
inStream.close();
response = sbResponse.toString();
System.out.println(response);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = JSON.parseObject(response, clazz);
System.out.println("請求服務器成功,做相應處理");
} else {
result = null;
System.out.println("請求服務端失敗");
}
} catch (Exception e) {
System.out.println("請求異常");
throw new RuntimeException(e);
}
return result;
}