• 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();
        }
  • 相关阅读:
    [你必须知道的.NET]第二十五回:认识元数据和IL(中)
    [技术速递]体验微软开源范例Oxite
    一本去繁存精的设计书《C# 3.0设计模式》
    [你必须知道的.NET]第二十四回:认识元数据和IL(上)
    泛型KMP算法
    实现语音视频录制(demo源码)
    AutoResetEvent 的诡异行为
    调用非托管dll常出现的bug及解决办法
    如何实现离线文件?
    广播与P2P通道(上) 问题与方案
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/15769588.html
Copyright © 2020-2023  润新知