zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    技术汇总:第四章:使用Easyui做三级下拉列表
    全文检索工具elasticsearch:第四章:开发电商的搜索列表功能
    全文检索工具elasticsearch:第三章: Java程序中的应用
    技术汇总:第三章:拖拉验证
    分布式部署:第三章:Docker、MySQL、tomcat、Nginx、Redis部署
    分布式部署:第二章:MyCat和Nginx部署
    分布式部署:第一章:zookeeper集群和solrcloud及redisCluster集群搭建
    技术汇总:第二章:JUC
    大型电商网站:第四章:业务功能与数据结构分析
    大型电商网站:第二章:项目开发介绍
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/15769588.html
Copyright © 2011-2022 走看看