java Http消息传递之POST和GET两种方法--通过实用工具类来获取服务器资源

简介: 实现该方法需要导入一些jar包 可以去一下地址下载: http://pan.baidu.com/s/1hqrJF7m   /** * 实用工具类来获取服务器资源 * * get方法传送数据 * * 1、通过path设定传送方式 * 2、创建客户端 * 3、得到输入流 * ...

实现该方法需要导入一些jar包

可以去一下地址下载:

http://pan.baidu.com/s/1hqrJF7m

 

/**
* 实用工具类来获取服务器资源
*
* get方法传送数据
*
* 1、通过path设定传送方式
* 2、创建客户端
* 3、得到输入流
* 4、读取流准备工作
* 5、读取并写入
* @throws IOException
* @throws ClientProtocolException
*
*/

 1     public static String getHttpResult(String path) throws ClientProtocolException, IOException{
 2     /*1、通过path设定传送方式*/
 3         
 4         HttpGet get=new HttpGet(path);
 5     /*2、创建客户端*/
 6         HttpClient client=new DefaultHttpClient();
 7         //通过get方式发送数据给服务器
 8         HttpResponse response=client.execute(get);
 9     /*3、得到输入流*/
10         if(response.getStatusLine().getStatusCode()==200){
11             InputStream in=response.getEntity().getContent();
12             
13     /*4、读取流准备工作*/
14             ByteArrayOutputStream bos=new ByteArrayOutputStream();
15             byte[]arr=new byte [1024];
16             int len=0;
17             
18     /*5、读取并写入*/
19             while((len=in.read(arr))!=-1){
20                 bos.write(arr, 0, len);
21             }
22             byte[]b=bos.toByteArray();
23             return new String(b,0,b.length);
24         }
25         
26         
27         
28         return null;
29     }

 

/**
* 实用工具类来获取服务器资源
*
* Post方法传送数据
*
* 1、通过path设定传送方式
* 2、创建客户端
* 3、得到输入流
* 4、读取流准备工作
* 5、读取并写入
* @throws IOException
* @throws ClientProtocolException
*
*/

 1 public static String getHttpResult(String path) throws ClientProtocolException, IOException{
 2     /*0、初始化要发送的数据用list存储*/
 3         List<NameValuePair> list=new ArrayList<NameValuePair>();
 4         list.add(new BasicNameValuePair("name", "zhangsan"));
 5         list.add(new BasicNameValuePair("name", "lisi"));
 6         list.add(new BasicNameValuePair("name", "wangwu"));
 7     /*1、通过path设定传送方式*/
 8         
 9         HttpPost post=new HttpPost(path);
10     /*2、创建客户端*/
11         HttpClient client=new DefaultHttpClient();
12         //通过post表单方式发送数据给服务器
13         
14         //建立表单
15         UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,"utf-8");
16         //装载到post中
17         post.setEntity(entity);
18         
19         HttpResponse response=client.execute(post);
20     /*3、得到输入流*/
21         if(response.getStatusLine().getStatusCode()==200){
22             InputStream in=response.getEntity().getContent();
23             
24     /*4、读取流准备工作*/
25             ByteArrayOutputStream bos=new ByteArrayOutputStream();
26             byte[]arr=new byte [1024];
27             int len=0;
28             
29     /*5、读取并写入*/
30             while((len=in.read(arr))!=-1){
31                 bos.write(arr, 0, len);
32             }
33             byte[]b=bos.toByteArray();
34             return new String(b,0,b.length);
35         }
36         
37         
38         
39         return null;
40     }
41     

 

相关文章
|
14天前
|
Java
java原生发送http请求
java原生发送http请求
|
1月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
163 0
|
3天前
|
XML Java 数据格式
Servlet 教程 之 Servlet 服务器 HTTP 响应 3
`Servlet`教程示例展示了如何创建一个HTTP响应,使用`@WebServlet(&quot;/Refresh&quot;)`的`Refresh`类继承`HttpServlet`。在`doGet`方法中,设置了`Refresh`头以每5秒自动刷新,并用`setContentType(&quot;text/html;charset=UTF-8&quot;)`设定内容类型。还使用`Calendar`和`SimpleDateFormat`获取并格式化当前时间显示。相应的`web.xml`配置指定了Servlet路径。当访问此Servlet时,页面将每5秒更新一次显示的系统时间。
14 4
|
7天前
|
网络协议 Java API
深度剖析:Java网络编程中的TCP/IP与HTTP协议实践
【4月更文挑战第17天】Java网络编程重在TCP/IP和HTTP协议的应用。TCP提供可靠数据传输,通过Socket和ServerSocket实现;HTTP用于Web服务,常借助HttpURLConnection或Apache HttpClient。两者结合,构成网络服务基础。Java有多种高级API和框架(如Netty、Spring Boot)简化开发,助力高效、高并发的网络通信。
|
1月前
|
Java 关系型数据库 MySQL
Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
【2月更文挑战第33天】Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
49 2
|
1月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
30 0
|
1月前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
162 1
|
1月前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
20 0
|
1月前
|
缓存
HTTP协议中,GET和POST有什么区别?分别适用什么场景?
HTTP协议中,GET和POST有什么区别?分别适用什么场景?
36 0