java Http消息传递之POST和GET两种方法

/**
* 通过Get方法来向服务器传值和获取信息,
* 这里举例假设的前提是,链接上服务器,服务器直接发送数据给本地
*
* 大体的思路:
* 1、首先通过URL地址来获得链接的借口
* 通过接口,来设置链接超时的时间,请求方式,是否可以输入输出数据
* 得到读取服务器内容的读取流
*
* 2、为存储 从服务器读取到的数据做准备
* 将读取到的数据写入文件或直接得到字符串
* 关闭并刷新读写流
*
*
*/

 1 public static void getMsgByGet(String path){
 2         try {
 3             /*为读取做准备*/
 4             
 5             //通过URL路径来创建URL对象
 6             URL url=new URL(path);
 7             //建立连接对象,并设置相应属性
 8             HttpURLConnection conn=(HttpURLConnection) url.openConnection();
 9             conn.setConnectTimeout(5000);
10             conn.setRequestMethod("GET");
11             conn.setDoInput(true);
12             //若连接成功获取输入流,并写入数据
13             if(conn.getResponseCode()==200){
14                 InputStream in=conn.getInputStream();
15             /*为写入做准备*/
16                 
17                 //设置存放数据的比特数组,
18                 byte[]arr=new byte[1024];
19                 //设置确定接收数组的长度的变量
20                 int len=0;
21                 //创建用来存放从服务器读取来的数据文件
22                 File file=new File("file\temp.txt");
23                 //创建写入流
24                 FileOutputStream fos=new FileOutputStream(file);
25                 
26             /* 开始读取和写入数据*/
27                 while((len=in.read(arr))!=-1){
28                     fos.write(arr,0,len);
29                 }
30                 fos.flush();
31             }
32             
33         } catch (MalformedURLException e) {
34             // TODO Auto-generated catch block
35             e.printStackTrace();
36         } catch (IOException e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }
40         
41     }

/**
*
* 通过Post方法向服务器发送数据和获取数据;
*
* 主要分
*
* 1、准备要发送到服务器的数据
* 2、为发送数据做准备
* 3、提交数据
* 4、为写入数据做准备
* 5、读取服务器返回的数据并写入
* @throws IOException
*
*
*/

 1 public String getMsg(String path) throws IOException{
 2         //这里发送的数据是一串字符串(你好呀)
 3         StringBuilder sb=new StringBuilder();
 4         sb.append("你好呀");
 5         
 6         
 7 /*为发送数据做准备*/
 8         
 9         //通过URL地址获取URL对象
10         URL url=new URL(path);
11         //获取链接对象
12         HttpURLConnection conn=(HttpURLConnection) url.openConnection();
13         //设置连接对象的属性
14         conn.setConnectTimeout(5000);
15         conn.setRequestMethod("POST");
16         conn.setDoInput(true);
17         conn.setDoOutput(true);
18         //设置提交数据类型(HTML传送数据必须的)
19         conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
20         //将要传递的数据转换为比特类型
21         byte[]data=sb.toString().getBytes();
22         //设置提交数据的长度
23         conn.setRequestProperty("Content-Length", String.valueOf(data.length));
24         
25 /*提交数据*/
26         OutputStream out=conn.getOutputStream();
27         out.write(data, 0, data.length);
28         out.close();
29         
30         //判断发送数据是否成功
31         if(conn.getResponseCode()==200){
32             InputStream in=conn.getInputStream();
33             
34 /*为写入数据做准备*/    
35             
36             ByteArrayOutputStream bos=new ByteArrayOutputStream();
37             
38             byte []arr=new byte[1024];
39             int len=0;
40             
41 /*读取服务器返回的数据并写入*/
42             while((len=in.read(arr))!=-1){
43                 bos.write(arr, 0, len);
44             }
45             byte[]b=bos.toByteArray();
46             return new String(b,0,b.length,"utf-8");
47         }
48 
49         
50         return null;
51         
52     }
原文地址:https://www.cnblogs.com/zxxiaoxia/p/4319312.html