• httpclient 发送请求,带中文信息


    有些功能 通过浏览器或者postman来代用没问题。可是有时候通过java代码来调用,就容易出现中文乱码


    通过httpclient的get方法发送中文

    String location = "嘻哈";
    		CloseableHttpClient httpClient = HttpClients.createDefault();// 创建http实例
    		CloseableHttpResponse response = null;
    		HttpGet httpGet =new HttpGet("http://127.0.0.1:8080/ServiceLocationSwitch/LocationSwitch/locationToLatAndLngForInternal.html?data="+location+"");
     	    httpGet.setHeader("Content-Type","application/html; charset=UTF-8");
     	   response = httpClient.execute(httpGet);
    		HttpEntity entity = response.getEntity(); //请求类型
    		String content = EntityUtils.toString(entity, "utf-8");
    		System.out.println(content);
            response.close();
    		httpClient.close();
    		





    通过httpclient的post方法发送中文


    String location ="中国";
    	
    	CloseableHttpClient httpClient = HttpClients.createDefault();// 创建http实例
    	HttpPost httpPost =new HttpPost();
          httpPost.setURI(new URI("http://127.0.0.1:8080/ServiceLocationSwitch/LocationSwitch/locationToLatAndLngForInternal.html"));
    	
          JSONObject object =new JSONObject();
          object.put("location", location);
          
    	List<NameValuePair> parms = new ArrayList<NameValuePair>();
    	parms.add(new BasicNameValuePair("data", object.toJSONString()));
    	httpPost.setEntity(new UrlEncodedFormEntity(parms,"utf-8"));
    	
    	CloseableHttpResponse response = httpClient.execute(httpPost);
    	HttpEntity entity = response.getEntity(); //请求类型
    	String content = EntityUtils.toString(entity, "utf-8");
    	System.out.println(content);
    	 response.close();
    	 httpClient.close();

  • 相关阅读:
    HDU 1000 A + B Problem
    HDU 3635 Dragon Balls
    HDU 3461 Code Lock
    HDU 1856 More is better
    HDU 1198 Farm Irrigation
    HDU 1325 Is It A Tree?
    HDU 1001 Sum Problem
    HDU 1829 A Bug's Life
    HDU 2610 Sequence one
    HDU 3350 #define is unsafe
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713859.html
Copyright © 2020-2023  润新知