java emulates Get and Post requests from http and sets the methods of ip and port proxies
- 2020-06-07 04:35:32
- OfStack
This paper covers three basic points:
1. Because many companies have agents on their internal networks, the browser accesses the Internet through ip and port, and the java code simulates http get, which also requires an external network agent;
2. Java implements Get/Post request code of http;
3. Setting the attributes in the header of HttpURLConnection request
Cookie, ES20en-ES21en (browser type), and so on.
For example: add Header to the http request
conn.setRequestProperty("Authorization", authorization);
Note: I found 1 section of Get/Post simulation request code on the Internet, added the configuration of the agent, and the integration was completed.
package com.pasier.quanzi.web.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
public static void main(String[] args) {
// If not set, as long as the proxy IP And the proxy port is correct , This option is not set
System.getProperties().setProperty("http.proxyHost", "10.22.40.32");
System.getProperties().setProperty("http.proxyPort", "8080");
// Determines whether the agent was set up successfully
// send GET request
System.out.println(sendGet(
"http://www.baidu.com",
"param1=xxx¶m2=yyy"));
// send POST request
}
/**
* To specify the URL send GET Method request
*
* @param url
* Sending a request URL
* @param param
* Request parameter, the request parameter should be name1=value1&name2=value2 In the form.
* @return URL The response result of the remote resource that represents
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// Open and URL The connection between
URLConnection connection = realUrl.openConnection();
// Sets the generic request properties
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// Establish the actual connection
connection.connect();
// Gets all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// Iterate through all the response header fields
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// define BufferedReader Input stream to read URL The response of the
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(" send GET Request exception! " + e);
e.printStackTrace();
}
// use finally Block to close the input stream
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* To specify the URL send POST Method request
*
* @param url
* Sending a request URL
* @param param
* Request parameter, the request parameter should be name1=value1&name2=value2 In the form.
* @return The response result of the remote resource that represents
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// Open and URL The connection between
URLConnection conn = realUrl.openConnection();
// Sets the generic request properties
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// send POST The request must be set to the following two lines
conn.setDoOutput(true);
conn.setDoInput(true);
// To obtain URLConnection Object corresponding output stream
out = new PrintWriter(conn.getOutputStream());
// Send request parameters
out.print(param);
// flush Output stream buffering
out.flush();
// define BufferedReader Input stream to read URL The response of the
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(" send POST Request exception! " + e);
e.printStackTrace();
}
// use finally Block to close the output stream, the input stream
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}