简述:
使用Java写 向后台服务做GET和POST请求
代码:
Test1.java
建立Http Connection, 向后台的Servlet做出Get请求
package test.java_request; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; class StreamToString{ public static String ConvertToString(InputStream inputStream){ InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder result = new StringBuilder(); String line = null; try { while((line = bufferedReader.readLine()) != null){ result.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try{ inputStreamReader.close(); inputStream.close(); bufferedReader.close(); }catch(IOException e){ e.printStackTrace(); } } return result.toString(); } public static String ConvertToString(FileInputStream inputStream){ InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder result = new StringBuilder(); String line = null; try { while((line = bufferedReader.readLine()) != null){ result.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try{ inputStreamReader.close(); inputStream.close(); bufferedReader.close(); }catch(IOException e){ e.printStackTrace(); } } return result.toString(); } } public class Test1 { public static void main(String[] args) { try{ URL url = new URL("http://localhost:8090/MyWebProject/Test"); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //GET Request Define: urlConnection.setRequestMethod("GET"); urlConnection.connect(); //Connection Response From Test Servlet System.out.println("Connection Response From Test Servlet"); InputStream inputStream = urlConnection.getInputStream(); //Convert Stream to String String responseStr = StreamToString.ConvertToString(inputStream); System.out.println(responseStr); }catch(IOException e){ Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, e); } } }
输出1: (GET)
Test2.java
通过修改urlConnection RequestMethod来做出POST的请求
进而向后台做Post请求
package test.java_request; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; public class Test2 { public static void main(String[] args) { try{ URL url = new URL("http://localhost:8090/MyWebProject/Test"); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //POST Request Define: urlConnection.setRequestMethod("POST"); urlConnection.connect(); //Connection Response From Test Servlet System.out.println("Connection Response From Test Servlet"); InputStream inputStream = urlConnection.getInputStream(); //Convert Stream to String String responseStr = StreamToString.ConvertToString(inputStream); System.out.println(responseStr); }catch(IOException e){ Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, e); } } }
后台服务的代码:
TestSertlet.java
package test.java_request; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; public class Test2 { public static void main(String[] args) { try{ URL url = new URL("http://localhost:8090/MyWebProject/Test"); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //Post Request Define: urlConnection.setRequestMethod("POST"); urlConnection.connect(); //Connection Response From Test Servlet System.out.println("Connection Response From Test Servlet"); InputStream inputStream = urlConnection.getInputStream(); //Convert Stream to String String responseStr = StreamToString.ConvertToString(inputStream); System.out.println(responseStr); }catch(IOException e){ Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, e); } } }
输出2:(POST)
小结:
从代码试写中,感觉建立和后台的连接之后,通过数据流的传递,得到后台的response,进而做下一步
通过修改urlConnection 的RequestMethod , 告诉后台Servlet 需要那个接口来接受客户端的请求
从而可以修改, “POST” 请求