1 packagecom.jiucool.www.struts.action;2
3 importjava.io.BufferedReader;4 importjava.io.DataOutputStream;5 importjava.io.File;6 importjava.io.FileReader;7 importjava.io.IOException;8 importjava.io.InputStreamReader;9 importjava.net.HttpURLConnection;10 importjava.net.URL;11 importjava.net.URLEncoder;12
13 public classpost_request {14 public static final String GET_URL = "http://www.cngolon.com/request.action?key=j0r56u2";15 public static final String POST_URL = "http://www.cngolon.com/request.action";16 //get()请求
17 public static void readContentFromGet() throwsIOException {18 //拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
19 String getURL =
20 GET_URL + "&activatecode=" + URLEncoder.encode("中国聚龙", "utf-8");21 URL getUrl = newURL(getURL);22 //根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,23 //返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
24 HttpURLConnection connection =(HttpURLConnection) getUrl.openConnection();25 //进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到26 //服务器
27 connection.connect();28 //取得输入流,并使用Reader读取
29 BufferedReader reader = newBufferedReader(30 new InputStreamReader(connection.getInputStream(), "utf-8")31 );32 //设置编码,否则中文乱码
33 System.out.println("=============================");34 System.out.println("Contents of get request");35 System.out.println("=============================");36 String lines;37 while ((lines = reader.readLine()) != null) {38 //lines = new String(lines.getBytes(), "utf-8");
39 System.out.println(lines);40 }41 reader.close();42 //断开连接
43 connection.disconnect();44 System.out.println("=============================");45 System.out.println("Contents of get request ends");46 System.out.println("=============================");47 }48 //post()请求
49 public static void readContentFromPost() throwsIOException {50 //Post请求的url,与get不同的是不需要带参数
51 URL postUrl = newURL(POST_URL);52 //打开连接
53 HttpURLConnection connection =(HttpURLConnection) postUrl.openConnection();54 //Output to the connection. Default is55 //false, set to true because post56 //method must write something to the57 //connection58 //设置是否向connection输出,因为这个是post请求,参数要放在59 //http正文内,因此需要设为true
60 connection.setDoOutput(true);61 //Read from the connection. Default is true.
62 connection.setDoInput(true);63 //Set the post method. Default is GET
64 connection.setRequestMethod("POST");65 //Post cannot use caches66 //Post 请求不能使用缓存
67 connection.setUseCaches(false);68 //This method takes effects to69 //every instances of this class.70 //URLConnection.setFollowRedirects是static函数,作用于所有的URLConnection对象。71 //connection.setFollowRedirects(true);72 //This methods only73 //takes effacts to this74 //instance.75 //URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
76 connection.setInstanceFollowRedirects(true);77 //Set the content type to urlencoded,78 //because we will write79 //some URL-encoded content to the80 //connection. Settings above must be set before connect!81 //配置本次连接的Content-type,配置为application/x-www-form-urlencoded的82 //意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode83 //进行编码
84 connection.setRequestProperty(85 "Content-Type",86 "application/x-www-form-urlencoded"
87 );88 //连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,89 //要注意的是connection.getOutputStream会隐含的进行connect。
90 connection.connect();91 DataOutputStream out = newDataOutputStream(connection.getOutputStream());92 //The URL-encoded contend93 //正文,正文内容其实跟get的URL中'?'后的参数字符串一致
94 String content =
95 "key=j0r53nmbbd78x7m1pqml06u2&type=1&toemail=cngolon@gmail.com" +
96 "&activatecode=" +
97 URLEncoder.encode("中国聚龙", "utf-8");98 //DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
99 out.writeBytes(content);100 out.flush();101 out.close();102 //flush and close
103 BufferedReader reader = newBufferedReader(104 new InputStreamReader(connection.getInputStream(), "utf-8")105 );106 //设置编码,否则中文乱码
107 String line = "";108 System.out.println("=============================");109 System.out.println("Contents of post request");110 System.out.println("=============================");111 while ((line = reader.readLine()) != null) {112 //line = new String(line.getBytes(), "utf-8");
113 System.out.println(line);114 }115 System.out.println("=============================");116 System.out.println("Contents of post request ends");117 System.out.println("=============================");118 reader.close();119 connection.disconnect();120 }121 }