Java implementation sends Get and Post requests

preface I haven't written a blog for several...
preface
Summary and classification
code

preface

I haven't written a blog for several days, because there are a lot of things in my life recently. Although I am learning every day, I haven't recorded the knowledge points in my blog, which is very bad

  • In the future, we must stick to blogging and record our growth
  • I hope I can find some like-minded friends here to work hard and make progress together

Summary and classification

  1. Java creating proxy connection object
  2. Java sends Post request
  3. Java send Get request

code

Java creating proxy connection object

/** * 1.Return proxy object * @param proxyIp * @param proxyPort * @return */ public Proxy setProxy(String proxyIp , int proxyPort ){ try{ InetSocketAddress socketAddress = new InetSocketAddress(proxyIp , proxyPort ); Proxy proxy = new Proxy(Proxy.Type.HTTP , socketAddress ); return proxy; }catch(Exception e ){ e.printStackTrace(); } return null; }

Java send Get request

/** * 2.Send Get request * @param url * @param params * Indicates some parameters after the link, such as name = ghoset & pass = ghoset * @return */ public String sendGet(String url , String params ) throws Exception { StringBuilder builder = new StringBuilder(); if(params != null || params.length() != 0 ){ url = url + "?" + params; //Restructure URL links } URL Url = new URL(url ); URLConnection conn = Url.openConnection(); // Set up proxy //URLConnection conn = Url.openConnection(setProxy(proxyHost, proxyPort)); // Add the following line if you need to set the proxy account password //conn.setRequestProperty("Proxy-Authorization", "Basic "+Base64.encode("account:password".getBytes())); //Send packets (you can grab browser packets and copy them directly) conn.setRequestProperty("accept", "*/*" ); conn.setRequestProperty("Connection", "Keep-Alive" ); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"); conn.connect(); //Receive response packets Map<String , List<String > > map = conn.getHeaderFields(); Set<String > set = map.keySet(); for(String k : set ){ String v = conn.getHeaderField(k ); System.out.println(k + ":" + v ); } //Return output information of browser BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream() )); String line = reader.readLine(); line = new String(line.getBytes() , "gbk" ); //The implementation converts the string to gbk type display while(line != null ){ builder.append(line +"\r\n" ); System.out.println(line ); line = reader.readLine(); } //Release resources reader.close(); return builder.toString(); }

Java sends Post request

/** * 3.Send POST request * @param url * @param params * @param forData * @return * @throws Exception */ public String sendPost(String url , String params , String formData) throws Exception{ StringBuilder builder = new StringBuilder(); if(!(params == null || params.length() == 0) ){ url += ("?" + params ); } URL Url = new URL(url ); URLConnection conn = Url.openConnection(); //If you set up a proxy, it's the same as sending a GET conn.setRequestProperty("accept", "*/*" ); conn.setRequestProperty("Connection", "Keep-Alive" ); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"); //After setting up, you can send the POST request conn.setDoInput(true ); conn.setDoOutput(true ); //Get its output stream and write directly to the post request PrintWriter writer = new PrintWriter(conn.getOutputStream() ); writer.print(formData ); writer.flush(); //Get browser's return data BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream() ) ); String line = reader.readLine(); line = new String(line.getBytes() , "utf-8" ); //Solve the problem of garbled code while(line != null ){ System.out.println(line ); builder.append(line + "\r\n" ); line = reader.readLine(); } reader.close(); writer.close(); return builder.toString(); }

test method

public static void main(String[] args) { HttpRequestUtil request = new HttpRequestUtil(); try{ Proxy proxy = request.setProxy(InetAddress.getLocalHost().getHostAddress() , 8888 ); System.out.println(proxy ); //Get request request.sendGet("http://localhost/review/html/index.php", "username=ghoset&password=ghoset" ); //Post request String formData = "username=ghoset&password=ghoset"; String rs = request.sendPost("http://localhost/review/java/index.php", null , formData ); }catch(Exception e ){ e.printStackTrace(); }

19 May 2020, 11:01 | Views: 8712

Add new comment

For adding a comment, please log in
or create account

0 comments