httpClient 访问其他Web 获取信息

本实例采用HttpClient技术,通过Json处理数据,实例代码:

发送方发送并接收数据:

public static JSONObject postHttpClient(String url,JSONObject json) throws Exception{
		//创建默认的 HttpClient 实例
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        JSONObject response = null;
        try {
        	StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			httpPost.setEntity(s);
			System.out.println("execurting request:" + httpPost.getURI());
			
            HttpResponse httpResponse = null;
            httpResponse = httpClient.execute(httpPost);
            System.out.println(httpResponse.getStatusLine().getStatusCode());
            //接收数据
	    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            	HttpEntity entity = httpResponse.getEntity();
				String charset = EntityUtils.getContentCharSet(entity);
				String jsonStr = "";
				BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),charset));
				String temp = reader.readLine();
				 while(temp!=null){
					 jsonStr+=temp;
					 temp = reader.readLine();
				}
				response = JSONObject.fromObject(jsonStr);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.debug(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.debug(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.debug(e.getMessage());
        } finally {
            //关闭连接,释放资源
            httpClient.getConnectionManager().shutdown();
        }
        return response;
	}

 接收方接收数据并返回:

	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StringBuffer sb = new StringBuffer();
		String line = null;
		List<XxtsDomain> dataList = null;
		try {
			BufferedReader reader = request.getReader();
			while ((line = reader.readLine()) != null){
				sb.append(line);
			}
			JSONObject jsonObject = JSONObject.fromObject(sb.toString());
			JSONArray array = jsonObject.getJSONArray("dataList");
			dataList = JSONArray.toList(array, new XxtsDomain(), new JsonConfig());
			for (XxtsDomain xxtsDomain: dataList) {
				if(postTsxxOne(xxtsDomain)){
					xxtsDomain.setSendBool(true);
				}else{
					xxtsDomain.setSendBool(false);
				}
			}
		} catch (Exception e) {}
		
		// 输出
         response.setContentType("text/html;charset=UTF-8");   
        PrintWriter out = response.getWriter();
        XxtsAllDomain xxtsAll = new XxtsAllDomain();
	xxtsAll.setDataList(dataList);
	JSONObject jsonList = JSONObject.fromObject(xxtsAll);  
        out.println(jsonList.toString());
	out.flush();
        out.close();
	}   	

 json 部分可参考另一篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值