zoukankan      html  css  js  c++  java
  • HttpClient---------demo

    public class aa {
        public static void main(String[] args) {
            // 创建HttpClient实例
            HttpClient httpclient = new DefaultHttpClient();
            // 创建Get方法实例
            HttpPost httpPost = new HttpPost(
                    "http://localhost:8080/sso/modify/modify.action");
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            // 提交两个参数及值
            nvps.add(new BasicNameValuePair("data", "wwwq"));
            // 设置表单提交编码为UTF-8
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                HttpResponse response = httpclient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instreams = entity.getContent();
                    String str = convertStreamToString(instreams);
                    System.out.println("Do something");
                    System.out.println(str);
                    // Do not need the rest
                }
            } catch (Exception e) {
                  e.printStackTrace();
            }finally{
                httpPost.abort();
            }
        }
    
        public static String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
    
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "
    ");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    }
  • 相关阅读:
    RSA算法
    本地CodeForces
    基于Giolite 多人开发
    DES算法
    MD5算法
    仓库库存管理系统(C+MySQL+ODBC)
    Spring源码解析(一)开篇
    JDK动态代理实现源码分析
    J.U.C Atomic(一)CAS原理
    JDK 注解详解
  • 原文地址:https://www.cnblogs.com/volare/p/3958283.html
Copyright © 2011-2022 走看看