欢迎来到Introzo百科
Introzo百科
HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
-->
*
* username=输入值 password=输入值
*/
1.转自:https://www.introzo.com/alinshen/article/details/78221567?utm_source=blogxgwz4
/*
* 演示通过HttpClient get请求的方式获取服务器的返回数据
*/
- public class HttpClientDemo {
- public static void main(String[] args) throws ClientProtocolException, IOException {
- String path="http://10.0.184.105:58080/ServletDemo4/LoginServlet?username=admin&password=admin";
- //1.创建客户端访问服务器的httpclient对象 打开浏览器
- HttpClient httpclient=new DefaultHttpClient();
- //2.以请求的连接地址创建get请求对象 浏览器中输入网址
- HttpGet httpget=new HttpGet(path);
- //3.向服务器端发送请求 并且获取响应对象 浏览器中输入网址点击回车
- HttpResponse response=httpclient.execute(httpget);
- //4.获取响应对象中的响应码
- StatusLine statusLine=response.getStatusLine();//获取请求对象中的响应行对象
- int responseCode=statusLine.getStatusCode();//从状态行中获取状态码
- if(responseCode==200){
- //5.获取HttpEntity消息载体对象 可以接收和发送消息
- HttpEntity entity=response.getEntity();
- //EntityUtils中的toString()方法转换服务器的响应数据
- String str=EntityUtils.toString(entity, "utf-8");
- System.out.println("服务器的响应是:"+str);
- // //6.从消息载体对象中获取操作的读取流对象
- // InputStream input=entity.getContent();
- // BufferedReader br=new BufferedReader(new InputStreamReader(input));
- // String str=br.readLine();
- // String result=new String(str.getBytes("gbk"), "utf-8");
- // System.out.println("服务器的响应是:"+result);
- // br.close();
- // input.close();
- }else{
- System.out.println("响应失败!");
- }
- }
- }
/*
* 演示HttpClient使用Post提交方式提交数据
*
*
* username=输入值 password=输入值
*/
- public class HttpClientDemo4 {
- public static void main(String[] args) throws ClientProtocolException, IOException {
- String baseUrl="http://10.0.184.105:58080/ServletDemo4/LoginServlet";//username=? password=?
- HttpClient httpclient=new DefaultHttpClient();
- //以请求的url地址创建httppost请求对象
- HttpPost httppost=new HttpPost(baseUrl);
- //NameValuePair 表示以类的形式保存提交的键值对
- NameValuePair pair1=new BasicNameValuePair("username", "ad");
- NameValuePair pair2=new BasicNameValuePair("password", "admin");
- //集合的目的就是存储需要向服务器提交的key-value对的集合
- List
listPair=new ArrayList (); - listPair.add(pair1);
- listPair.add(pair2);
- //HttpEntity 封装消息的对象 可以发送和接受服务器的消息 可以通过客户端请求或者是服务器端的响应获取其对象
- HttpEntity entity=new UrlEncodedFormEntity(listPair);//创建httpEntity对象
- httppost.setEntity(entity);//将发送消息的载体对象封装到httppost对象中
- HttpResponse response=httpclient.execute(httppost);
- int responseCode=response.getStatusLine().getStatusCode();
- if(responseCode==200){
- //得到服务器响应的消息对象
- HttpEntity httpentity=response.getEntity();
- System.out.println("服务器响应结果是:"+EntityUtils.toString(httpentity, "utf-8"));
- }else{
- System.out.println("响应失败!");
- }
- }
- }
相关文章
- 10-05 戈多的场景树
- 10-05 戈多动画
- 10-05 在 Godot 中设计标题画面
- 10-05 信息搜索和可视化
- 10-05 设计流程与任务分析
- 10-05 颤动警报对话框
- 10-05 PostgreSQL远程连接配置管理/账号密码分配
- 10-05 Windows server 创建FTP 包括ft
- 10-05 Mongodb副本集加分片集群安全认证使用账号密码
- 10-05 浅谈ubuntu中执行.sh文件的几种方式的区别
- 10-05 Linux性能优化的实用思路和技巧(linux性能
- 10-05 如何轻松安装Linux系统显卡驱动(Linux安装
- 10-05 win10动态锁设置教程
- 10-05 win10关闭Win10右下角提示的教程
- 10-05 win10设置定时提醒闹钟方法
- 10-05 win10音频服务未运行 错误1068怎么办
- 10-05 win10哪里下载
- 10-05 Win10命令提示符打不开怎么办
- 10-05 实现发送模板消息功能的微信小程序示例【通过open
- 10-05 微信公众平台发送模板消息(Java接口开发)
- 最近发表