当前位置: 首页>>代码示例>>Java>>正文


Java StringEntity.setContentEncoding方法代码示例

本文整理汇总了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;
}
 
开发者ID:Evan1120,项目名称:wechat-api-java,代码行数:24,代码来源:HttpRequestUtil.java

示例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;
}
 
开发者ID:venus-boot,项目名称:saluki,代码行数:24,代码来源:ServiceTestController.java

示例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();
}
 
开发者ID:MinsxCloud,项目名称:minsx-java-example,代码行数:34,代码来源:RemoteServerClientImpl.java

示例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);
}
 
开发者ID:wolfboys,项目名称:opencron,代码行数:12,代码来源:HttpClientUtils.java

示例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;
}
 
开发者ID:gongjunhao,项目名称:car-bjpermit,代码行数:37,代码来源:HttpClientUtils.java

示例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);
}
 
开发者ID:1991wangliang,项目名称:lorne_core,代码行数:18,代码来源:HttpUtils.java

示例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;
}
 
开发者ID:liuxx001,项目名称:bird-java,代码行数:56,代码来源:HttpHelper.java


注:本文中的org.apache.http.entity.StringEntity.setContentEncoding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。