向微信发送post请求工具类

    public static Map<String,Object> getResJson(String postUrl,String reqJson) throws UnsupportedEncodingException{    
        //发送请求
        HttpClient client =new DefaultHttpClient();
        HttpPost post=new HttpPost(postUrl);
        //参数
        if(!reqJson.equals("")){
            //转码
            System.out.println("buweidkong");
            System.out.println(reqJson);
            StringEntity se = new StringEntity(new String(reqJson.getBytes("utf-8"),"ISO-8859-1"));
            se.setContentType("application/json;charset=ISO-8859-1");
            post.setEntity(se);
        }
        // 初始化解析json格式的对象
        JsonParser jsonparer = new JsonParser();
        Map<String,Object> map = new HashMap<String,Object>();
        HttpResponse response = null;
        JsonObject responseJson = null;
        try {
            //执行请求,获取响应
            response=client.execute(post);
            //处理结果集
            HttpEntity entity=response.getEntity();
            //响应文本
            String responseContent=EntityUtils.toString(entity);
            System.out.println("响应"+responseContent);
            //将响应文本解析为json格式,转码
            responseJson=jsonparer.parse(new String(responseContent.getBytes("ISO-8859-1"),"utf-8")).getAsJsonObject();
        }catch(IOException e){
            e.printStackTrace();
        }
        map.put("response", response);
        map.put("responseJson", responseJson);
        return map;
    }

 

posted @ 2018-03-16 10:58  radiant13  阅读(950)  评论(0)    收藏  举报