HttpClient psot和get请求

    private String backAllUserInfo(String uid) throws IOException {
        //this.setInterfaceurl("/idm/jsoninterface/userManager/getUserByUid.do");
        this.setInterfaceurl("/api/user/get/userinfobyloginname.do?loginName="+uid);//如果是post去掉url后参数放到下面NameValuePair里面传递
        String timestamp=String.valueOf(System.currentTimeMillis());
        String response = "";
        GetMethod postMethod = new GetMethod(getInterfaceURL());//如果是post请求则GetMethod换成PostMethod
        HttpMethodParams parms = new HttpMethodParams();
        parms.setContentCharset("UTF-8");

        // 在header中放入用户名和密码
        //NameValuePair[] data = { new NameValuePair("loginName",uid) };

        //postMethod.setParams(parms);
        //postMethod.setRequestHeader("loginName", uid);
        //postMethod.setRequestHeader("password",
                //"A722C63DB8EC8625AF6CF71CB8C2D939");
        postMethod.setRequestHeader("timestamp", timestamp);
        postMethod.setRequestHeader("token",getToken("D73CABDB3B5F085523C46D3CFAC3EB3B8BB9", "netgate_test",timestamp ));
        postMethod.setRequestHeader("appid","netgate_test");
        InputStream inputStream = null;
        BufferedReader br =null;
        try {
            //postMethod.setRequestBody(data);
            HttpClient client = new HttpClient(new HttpClientParams(),
                    new SimpleHttpConnectionManager(true));
            //设置连接超时时时间
            client.getHttpConnectionManager().getParams()
                    .setConnectionTimeout(10000);
            //设置读取数据超时时时间
            client.getHttpConnectionManager().getParams().setSoTimeout(10000);
            // 设置连接时间
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                inputStream = postMethod.getResponseBodyAsStream();
                br = new BufferedReader(new InputStreamReader(
                        inputStream, "UTF-8"));
                StringBuffer stringBuffer = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    stringBuffer.append(str);
                }
                response = stringBuffer.toString();
            } else {
                response = "";
            }
        } catch (Exception e) {
            super.setError(STATU_CONNERROR);
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
            }
            // 释放连接
            postMethod.releaseConnection();
        }
        return response;
    }
    public String getInterfaceURL() {
        Config config = Config.getInstance();
        String ip = config.getValue("idm.ip");
        String port = config.getValue("idm.port");
        StringBuffer buffer = new StringBuffer();
        buffer.append("http://");
        buffer.append(ip);
        buffer.append(":");
        buffer.append(port);
        buffer.append(this.getInterfaceurl());
        return buffer.toString();
    }
原文地址:https://www.cnblogs.com/cuijinlong/p/15769588.html