当前位置: 首页 > news >正文

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 + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

 

转载于:https://www.cnblogs.com/volare/p/3958283.html

相关文章:

  • SQLSERVER存储过程基本语法
  • 安装redis
  • C#实现一个最简单的HTTP服务器
  • 【特别推荐】14个支持响应式设计的流行前端开发框架
  • 2012毕业找工作记录点滴
  • FAQ_Zabbix:解决模板收集到的数据和真实数据有偏差
  • 使用钩子函数[2]
  • 【从零之三(更)】自定义类中调用讯飞语音包错误解决办法
  • spring SpEL
  • view动画库
  • Submit a form with Ajax 发送邮件参考
  • 应该总结自己了
  • Tomcat下载安装及常见问题解决办法
  • 在共享文件夹中解压到linux其他目录中,千万注意软连接的问题
  • (黑客游戏)HackTheGame1.21 过关攻略
  • 「译」Node.js Streams 基础
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • Apache Zeppelin在Apache Trafodion上的可视化
  • bootstrap创建登录注册页面
  • C++类的相互关联
  • HTTP请求重发
  • Python进阶细节
  • ReactNativeweexDeviceOne对比
  • springMvc学习笔记(2)
  • webpack入门学习手记(二)
  • win10下安装mysql5.7
  • 初探 Vue 生命周期和钩子函数
  • 构造函数(constructor)与原型链(prototype)关系
  • 蓝海存储开关机注意事项总结
  • 实现简单的正则表达式引擎
  • 使用putty远程连接linux
  • 鱼骨图 - 如何绘制?
  • Hibernate主键生成策略及选择
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • # 飞书APP集成平台-数字化落地
  • #AngularJS#$sce.trustAsResourceUrl
  • #前后端分离# 头条发布系统
  • $(selector).each()和$.each()的区别
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (转)Mysql的优化设置
  • (转)关于pipe()的详细解析
  • ./configure,make,make install的作用
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET 发展历程
  • .NET 反射 Reflect
  • .net(C#)中String.Format如何使用
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .NET与 java通用的3DES加密解密方法
  • .NET中 MVC 工厂模式浅析
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [4.9福建四校联考]
  • [④ADRV902x]: Digital Filter Configuration(发射端)