httpClient

http://hc.apache.org/html

Quick Start:http://hc.apache.org/httpcomponents-client-ga/index.htmljava

=================================web

 

SpringMVC 使用@ResponseBody返回json 中文亂碼spring

 

方法一,使用(produces = "application/json; charset=utf-8"):apache

@RequestMapping(value = "/for", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
	@ResponseBody
	public Object forr()
	{
		User user1 = new User();
		user1.setId(2);
		user1.setName("嘿嘿");
		user1.setPass("456");
		User user = new User(1,"張三","123",user1);
		String jsonString = JSON.toJSONString(user);
		return jsonString;
	}

  

方法二,在spring-mvc.xml中添加:json

<!-- 處理請求返回json字符串的中文亂碼問題 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

//get請求
	private static void doGet() throws IOException, ClientProtocolException {
		//建立一個 httpclient對象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		//建立一個Get請求				// http://localhost:8080/Shiro/for
		HttpGet httpGet = new HttpGet("http://localhsot:8080/Shiro/for");
		//HttpGet httpGet = new HttpGet("http://www.baidu.com?id =123&name=hehe");
		//執行請求
		CloseableHttpResponse response = httpclient.execute(httpGet);
		HttpEntity entity = response.getEntity();
		//獲得狀態碼
	   if (response.getStatusLine().getStatusCode() == 200)
	   {
		   System.out.println("---");
	   }
		//將HTTP響應體 轉換成String對象
		System.out.println( EntityUtils.toString(entity,"utf-8"));
	}

  

//get請求攜帶數據
		private static void doGetWithParm() throws IOException, ClientProtocolException, URISyntaxException {
			//建立一個 httpclient對象
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//建立一個Get請求
			URIBuilder uri = new URIBuilder("http://www.sogou.com/web");
			//添加查詢參數
			uri.addParameter("query","花千骨");
			HttpGet httpGet = new HttpGet(uri.build());
			//執行請求
			CloseableHttpResponse response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			//獲得狀態碼
		   if (response.getStatusLine().getStatusCode() == 200)
		   {
			   System.out.println("---");
		   }
			//將HTTP響應體 轉換成String對象
			System.out.println( EntityUtils.toString(entity,"utf-8"));
		}

  

// post請求
	public void doPost() throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 建立一個Post對象
		HttpPost httpPost = new HttpPost("http://targethost/login");
		// 執行post請求
		CloseableHttpResponse execute = httpclient.execute(httpPost);
		EntityUtils.toString(execute.getEntity(), "utf-8");
	}

  

// post請求攜帶參數
	public void doPostWithParm() throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 建立一個Post對象
		HttpPost httpPost = new HttpPost("http://targethost/login");
		//建立參數
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("username", "vip"));
		nvps.add(new BasicNameValuePair("password", "secret"));
		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
		// 執行post請求
		CloseableHttpResponse execute = httpclient.execute(httpPost);
		EntityUtils.toString(execute.getEntity(), "utf-8");
	}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息