Program components:
1. The client uses the eclipse HttpUtils.java standard java interface to implement HTTP post data submission. (submit username and password in post)
Focus point:
1. Public static String sendPostMessage(Map<String,String> params , String encode)
Purpose: The client sends the data params to the server, finally gets the input stream returned from the server, and finally converts the input stream into a string.Note how to use the standard java interface to implement the http post request, successfully connect to the server, and get the data returned from the server-side response.
2. Public String String changInputStream(InputStream inputStream , String encode)
Purpose: To convert an input stream into a string in the specified encoding.(In this case, the input stream InputStream returned from the server is converted to a string String encoding method encoding method)
3. Instantiation methods and iterative methods of Map<String ,String>
- Map's instantiation method:
123Map<String, String> params = new HashMap<String, String>();Params.put("username", "admin");Params.put("password", "123");
- The iterative method of Map:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
StringBuffer stringBuffer = new StringBuffer(); For (Map.Entry<String, String> entry : params.entrySet()) { Try { stringBuffer .append(entry.getKey()) .append("=") .append(URLEncoder.encode(entry.getValue(), encode)) .append("&"); } Catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Delete the last & character stringBuffer.deleteCharAt(stringBuffer.length() - 1); |
Program ideas:
- 1. The client establishes the http link httpURLConnection and uses OutputStream to send data to the server
- 2. Get the input stream InputStream returned from the server
- 3. Convert InputStream to String
-------------------------------------------------- -------------------------------------------------- ------------
Key code:
1. Client HttpUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
Package com.http.post; Import java.io.ByteArrayOutputStream; Import java.io.IOException; Import java.io.InputStream; Import java.io.OutputStream; Import java.io.UnsupportedEncodingException; 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 HttpUtils { // Indicates the server's url Private static String PATH = "http://192.168.0.100:8080/myhttp/servlet/LoginAction"; Private static URL url; Public HttpUtils() { // TODO Auto-generated constructor stub } Static { Try { Url = new URL(PATH); } Catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * params parameters to fill in the URL encode byte encoding */ Public static String sendPostMessage(Map<String, String> params, String encode) { StringBuffer stringBuffer = new StringBuffer(); If (params != null && !params.isEmpty()) { For (Map.Entry<String, String> entry : params.entrySet()) { Try { stringBuffer .append(entry.getKey()) .append("=") .append(URLEncoder.encode(entry.getValue(), encode)) .append("&"); } Catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Delete the last & character stringBuffer.deleteCharAt(stringBuffer.length() - 1); System.out.println("-->>" + stringBuffer.toString()); Try { HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(3000); httpURLConnection.setDoInput(true);//Get data from server httpURLConnection.setDoOutput(true);// Write data to server // The byte size and length of the upload information Byte[] mydata = stringBuffer.toString().getBytes(); // Set the request body type httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Lenth", String.valueOf(mydata.length)); // Get output stream, output data to server OutputStream outputStream = (OutputStream) httpURLConnection .getOutputStream(); outputStream.write(mydata); // Get the server response result and status code Int responseCode = httpURLConnection.getResponseCode(); If (responseCode == 200) { // Get input stream, get data from server InputStream inputStream = (InputStream) httpURLConnection .getInputStream(); Return (changeInputStream(inputStream, encode)); } } Catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Return ""; } /* * / / from the input stream InputStream to the specified encoding format encode into a string String */ Public static String changeInputStream(InputStream inputStream, String encode) { // ByteArrayOutputStream is generally called memory flow ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Byte[] data = new byte[1024]; Int len = 0; String result = ""; If (inputStream != null) { Try { While ((len = inputStream.read(data)) != -1) { byteArrayOutputStream.write(data, 0, len); } Result = new String(byteArrayOutputStream.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.put("password", "123"); String result = sendPostMessage(params, "utf-8"); System.out.println("-result->>" + result); } } |
This article was transferred from the CSDN blog, the original http://blog.csdn.net/neu_yousei/article/details/22486983, in order to respect the author's labor results, please indicate the source.Sitelink: https://wangbaiyuan.cn/en/gojava-implementation-http-post-to-submit-data-to-the-server-2.html, if you think this article is useful to you, you can click on "Sponsored Author" below the article to reward Author!
No Comment