zoukankan      html  css  js  c++  java
  • 使用httpClient发送post请求连接restful接口

    public static String httpPost(String url,String arg){
            InputStream is;
            BufferedReader br;
            StringBuilder sBuilder = null;
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("url");
                //方式一:将参数添加到请求体当中
                httpPost.setEntity(new StringEntity(arg,"utf-8"));//默认是采用ISO-8859-1 对于中文需要使用utf-8编码
                //方式二: 如果需要传递多个参数
                List<NameValuePair> parameters = new ArrayList<>();
                parameters.add(new BasicNameValuePair("name","value"));
                parameters.add(new BasicNameValuePair("name","value"));
                parameters.add(new BasicNameValuePair("name","value"));
                parameters.add(new BasicNameValuePair("name","value"));
                httpPost.setEntity(new UrlEncodedFormEntity(parameters));
                HttpResponse httpResponse = httpClient.execute(httpPut);
                //连接成功
                if(200 == httpResponse.getStatusLine().getStatusCode()) {
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                    br = new BufferedReader(new InputStreamReader(is));
                    String tempStr;
                    sBuilder = new StringBuilder();
                    while ((tempStr = br.readLine()) != null) {
                        sBuilder.append(tempStr);
                    }
                    br.close();
                    is.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return sBuilder==null? "":sBuilder.toString();
        }

    原地址:https://blog.csdn.net/u010989191/article/details/52862942

  • 相关阅读:
    js语法
    页面格式与布局
    css样式标签
    框架
    css样式表选择器
    最大值(东方化改题+老师给的题解)
    数字(东方化改题+老师给的正解)
    测试一下这个编辑器
    请让本题永远沉睡于此(东方化改题+给的标程)
    贪吃的yjj(东方化改题+给的标程)
  • 原文地址:https://www.cnblogs.com/chappell/p/9204921.html
Copyright © 2011-2022 走看看