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

  • 相关阅读:
    1. Change the emulator screen size
    Dynamic Programming for TSP
    框架的概念及用反射技术开发框架的原理
    【PHP 】 伪静态
    【PHP 】伪静态
    框架-Java:Spring MVC
    开源-解决方案-实时数据追踪:Zipkin 介绍
    报表-类型:瀑布图
    报表:目录
    软件-开发软件-Java-Eclipse:百科
  • 原文地址:https://www.cnblogs.com/chappell/p/9204921.html
Copyright © 2011-2022 走看看