Details of HTTP requests - request line
The GET in the request line is called the request method. The request methods include: POST, GET, HEAD, OPTIONS, DELETE, TRACE, PUT, and commonly used ones include: GET, POST
If the user does not have a setting, by default, the browser sends a get request to the server. For example, access is directly entered in the browser, access is clicked on the hyperlink, etc. are all gets. If the user wants to change the request method to post, he can change it. Implementation of form submission method.
Whether POST or GET is used to request a WEB resource from the server. The difference between these two methods is mainly reflected in data transmission: if the request method is GET method, it can be brought in the form of ? after the requested URL address. The data handed over to the server is separated by &, for example: GET /mail/1.html?name=abc&password=xyz HTTP/1.1
Features of the GET method: The parameters attached to the URL address are limited, and the data capacity usually cannot exceed 1K.
If the request method is POST, data can be sent to the server in the requested entity content. The characteristics of the Post method are: the amount of data transmitted is unlimited.
Details of HTTP requests - message header
Common message headers in HTTP requests
accept: The browser tells the server through this header, and the data type it supports Accept-Charset: The browser tells the server through this header, which character set it supports Accept-Encoding: The browser tells the server through this header, and the supported compression format Accept-Language: The browser tells the server through this header, and its locale Host: The browser tells the server through this header, which host to access If-Modified-Since: The browser tells the server through this header, and the time when the data is cached is Referer : The browser uses this header to tell the server, which page the client is from, Connection: The browser uses this header to tell the server, whether to disconnect the link or to whom the link is held after the request is completed.
example:
http_get
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.Mal formedURLException; import java.net.URL; public class Http_Get { private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png"; public Http_Get() { // TODO Auto-generated constructor stub } public static void saveImageToDi sk() { InputStream inputStream = getInputStream(); byte[] data = new byte[1024]; int len = 0; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("C://test.png"); while ((len = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (inputStre am != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fileOutputStream != null) { try { fileO utputStream.close(); } catch (IOException e ) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } /** * Get server-side data and return in the form of InputStream * @return */ public static InputStream getInpu tStream() { InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try { URL url = new URL(URL_PATH); if (url != null) { httpURLConnection = (HttpURLConnection) url.openConnection(); // Set the timeout time for connecting to the network httpURLConnection.setConnectTimeout(3000); httpURLConnection.setDoInput(true); // Indicates that this http request is set to request httpURLConnection.setRequestMethod("GET"); int responseCode = httpURLConnection. getResponseCode(); if (responseCode == 200) { // Get from the server An input stream inputStream = httpURLConnection.getInputStream(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace(); } return inputStream; } public static void main(String[] args) { // Get the image from the server and save it to local saveImageToDisk(); } } Http_Post
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Unsup portedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class Http_Post { // Request the url private stat on the server side ic String PATH = "http://192.168.1.125 :8080/myhttp/servlet/LoginAction"; private static URL url; public Http_Post() { // TODO Auto-generated constructor stub } static { try { url = new U RL(PATH); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param params * Parameters of the filled url* @param encode * Byte encoding* @return */ public static Strin g sendPostMessage(Map<String, String > params, String encode) { // StringBuffer buffer initialized as StringBuffer = new StringBuffer(); try { if (params != null && !params.isEmpty()) { for (Map. Entry<String, String> entry : params.entrySet()) { // Complete the transcoding operation buffer.append(entry.getKey()).append("=").append( URLEncoder.encode(entry.getValue(), encode)) .append : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ("&"); } buffer.deleteCharAt(buffer.length() - 1); } // System.out.println(buffer.toString()); // Delete the most& System.out.println( "-->>"+buffer.toString()); HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.setConnectTimeout(3000 ); urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true);/ / means to obtain data from the server urlConnection.setDoOutput(true);// means to write data to the server // Obtain the byte size and length of the uploaded information byte[] mydata = buffer.toString().getBytes(); // means to set The type of the request body is the text type urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConnection.setRequestProperty("Content- Length", String.valueOf(mydata.length)); // Get the output stream and output data to the server OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(mydata,0,mydata.length); outputStream.close() ; // Obtain the result and status code of the server response int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { return changeInputStream(urlConnection.getInputStream(), encode); } } catch (Unsup portedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } /** * Convert an input stream to a string with the specified encoding* * @param inputStream * @ param encode * @return */ private static String changeInputStream(InputStream inputStream, String encode) { // TODO Auto-generated method stub ByteArrayOutputStream outp utStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; String result = ""; if (inputStream != null) { try { while ((len = inputStream.read(data)) != -1) { outputStream.write(data, 0, len); } result = new String( outputStream.toByteArray(), encode); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } /** * @param args */ public static void main( String[] args) { // TODO Auto-generated method stub Map<String, String> params = new HashMap<String, String>(); params.put("username", "admin"); params ms.put(" password", "123"); String result = Http_Post.sendPostMessage(params, "utf-8"); System.out.println("--result->>" + result); } }