HttpUtils uses Post and Get to submit helper classes with parameters

foreword

This article does not explain, it is a tool class HttpUtils, which can be used directly!
	/**
	 * Send a GET request
	 *
	 * @param url
	 * Destination address
	 * @param parameters
	 * Request parameters, Map type.
	 * @return remote response result
	 */
	public static String sendGet(String url, Map<String, String> parameters) {
		String result = "";
		BufferedReader in = null;// Read the response input stream
		StringBuffer sb = new StringBuffer();// store parameters
		String params = "";// parameters after encoding
		try {
			// encode request parameters
			if (parameters.size() == 1) {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(java.net.URLEncoder.encode(parameters.get(name), "UTF-8"));
				}
				params = sb.toString();
			} else {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(java.net.URLEncoder.encode(parameters.get(name), "UTF-8")).append("&");
				}
				String temp_params = sb.toString();
				params = temp_params.substring(0, temp_params.length() - 1);
			}
			String full_url = url + "?" + params;
			System.out.println(full_url);
			// create URL object
			java.net.URL connURL = new java.net.URL(full_url);
			// Open the URL connection (a tcp connection to the server is established, no http request is actually sent!)
			URLConnection urlConnection=connURL.openConnection();
			java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) urlConnection;
			// Set the generic request attribute (if an attribute with this keyword already exists, overwrite its value with the new value.)
			httpConn.setRequestProperty("Accept", "*/*");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
			// Establish the actual connection (the remote object becomes available. The header fields and contents of the remote object become accessible)
			httpConn.connect();
			// get response header
			Map<String, List<String>> headers = httpConn.getHeaderFields();
			// loop through all response header fields
			for (String key : headers.keySet()) {
				System.out.println(key + "\t:\t" + headers.get(key));
			}
			// Define the BufferedReader input stream to read the response of the URL and set the encoding method
			in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
			String line;
			// read the returned content
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * Send POST request
	 *
	 * @param url
	 * Destination address
	 * @param parameters
	 * Request parameters, Map type.
	 * @return remote response result
	 */
	public static String sendPost(String url, Map<String, String> parameters) {
		String result = "";// Returned result
		BufferedReader in = null;// Read the response input stream
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer();// Process request parameters
		String params = "";// parameters after encoding
		try {
			// encode request parameters
			if (parameters.size() == 1) {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(java.net.URLEncoder.encode(parameters.get(name), "UTF-8"));
				}
				params = sb.toString();
			} else {
				for (String name : parameters.keySet()) {
					sb.append(name).append("=").append(java.net.URLEncoder.encode(parameters.get(name), "UTF-8")).append("&");
				}
				String temp_params = sb.toString();
				params = temp_params.substring(0, temp_params.length() - 1);
			}
			// create URL object
			java.net.URL connURL = new java.net.URL(url);
			// open URL connection
			java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
			// set common properties
			httpConn.setRequestProperty("Accept", "*/*");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
			// set the POST method
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			// Get the output stream corresponding to the HttpURLConnection object
			out = new PrintWriter(httpConn.getOutputStream());
			// send request parameters
			out.write(params);
			// flush the buffer of the output stream
			out.flush();
			// Define the BufferedReader input stream to read the response of the URL and set the encoding method
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			String line;
			// read the returned content
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782191&siteId=291194637