ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HttpClient psot和get请求

2022-01-06 09:04:28  阅读:123  来源: 互联网

标签:psot String get buffer setRequestHeader postMethod new append HttpClient


    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();
    }

 

标签:psot,String,get,buffer,setRequestHeader,postMethod,new,append,HttpClient
来源: https://www.cnblogs.com/cuijinlong/p/15769588.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有