zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    uniapp 小程序分享功能
    uniapp 输入有值后按钮变色
    css 跑马灯
    uniapp 复选框问题
    【Python】where cut query melt函数用法
    【Python】Pivot_table透视表用法及CategoricalDtype自定义排序
    【Python】Merge函数的用法
    【Python】extract及contains方法(正则提取筛选数据)
    Promise
    CSS垂直居中的方法
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713859.html
Copyright © 2011-2022 走看看