GetRequest
package com.net.http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class GetRequest { private String url; private String param; public GetRequest(String url,String param){ this.url=url; this.param=param; } public String sendRequest(){ String result=""; URL realUrl; try {
realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)"); conn.setDoOutput(true); conn.setDoInput(true); try(PrintWriter out =new PrintWriter(conn.getOutputStream())){ out.println(param); out.flush(); }
Map<String,List<String>> map= conn.getHeaderFields(); for(String key : map.keySet()){ System.out.println(key+ "---->"+map.get(key)); } try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"))){ String line; while((line=in.readLine())!=null){ result += " " + line; System.out.println(line); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
test类
package com.net.http; public class GetTest { public static void main(String[] args) { // TODO Auto-generated method stub new GetRequest("http://www.mocky.io/v2/574199bc120000d522a581ba","name=get").sendRequest(); } }
-------------------------------------------------------------------------------------------------------------------------------------------------------------
PostRequest
package com.net.http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Map; public class PostRequest { private String result =""; private String url; private String parm; private String urlName; public PostRequest(String url, String parm){ this.url=url; this.parm=parm; if(parm==null){ this.urlName=url; }else{ this.urlName=url+"?"+ parm; } } public String sendRequest(){ URL realUrl; try { realUrl = new URL(urlName);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)"); conn.connect(); Map<String,List<String>> map= conn.getHeaderFields(); for(String key : map.keySet()){ System.out.println(key+ "---->"+map.get(key)); } try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))){ String line; while((line=in.readLine())!=null){ result += " " + line; System.out.println(line); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
test类
package com.net.http; public class PostTest { public static void main(String[] args) { // TODO Auto-generated method stub new PostRequest("http://www.mocky.io/v2/574199bc120000d522a581ba",null).sendRequest(); } }