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();
        }
  • 相关阅读:
    vlc的应用之三:动态调用vlc-0.9.4的libvlc.dll【转】
    Linux 下搭建流媒体服务器
    linux bash Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
    lftp mirror 上传目录
    rxvt-unicode配置
    在命令行上 使用 mutt, fetchmail, maildrop, msmtp 收发邮件
    在命令行上 Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
    git查看各个branch之间的关系图
    在Ubuntu Server是配置iptables防火墙
    Ubuntu 14.04 配置iptables防火墙
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/15769588.html
Copyright © 2011-2022 走看看