This kind of problem has been encountered in production, and the interface call problem of java is recorded.
One is the json method;
public static String sendPost(String url, JSONObject obj)throws IOException{ OutputStreamWriter out = null; BufferedReader reader = null; String response=""; /* String paramStr = ""; if (null != param) { // Traverse the parameter Map and add it to the collection Set<String> keySet = param.keySet(); for (String key : keySet) { paramStr = paramStr + key + ":" + param.get(key) + "&"; } } int lastAndIndex = paramStr.lastIndexOf("&"); if (lastAndIndex != -1) { paramStr = paramStr.substring(0, lastAndIndex); } System.out.println("地址===" + url); System.out.println("入参===" + paramStr);*/ try { URL httpUrl = null ; // HTTP URL class uses this class to create connections // create URL httpUrl = new URL(url); // establish connection HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestProperty( "accept", "*/*"); // Set common request properties conn.setRequestProperty("Charset", "UTF-8" ); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("connection", "keep-alive"); conn.setUseCaches( false ); // Set not to cache conn.setInstanceFollowRedirects( true ); conn.setDoOutput(true); conn.setDoInput(true); conn.connect(); //POST请求 out = new OutputStreamWriter( conn.getOutputStream()); out.write(obj.toJSONString()); out.flush(); // Read response reader = new BufferedReader( new InputStreamReader( conn.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); response+=lines; } reader.close(); // Disconnect conn.disconnect(); } catch (Exception e) { System.out.println( "Exception sending POST request!"+ e); e.printStackTrace (); } // Use finally block to close output stream, input stream finally { try { if (out!= null ){ out.close(); } if(reader!=null){ reader.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return response; }
Another common str call interface;
/** * Send a POST request to the specified URL * @param url the URL to send the request to * @param param Request parameter, the request parameter should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by @return */ public static String sendPostWebReq(String url, Map<String, Object> param, int i) throws Exception { String paramStr = "" ; if ( null != param) { // Traverse the parameter Map and add it to the collection Set<String> keySet = param.keySet(); for (String key : keySet) { paramStr = paramStr + key + "=" + param.get(key) + "&"; } } int lastAndIndex = paramStr.lastIndexOf("&"); if (lastAndIndex != -1) { paramStr = paramStr.substring(0, lastAndIndex); } System.out.println("地址===" + url); System.out.println("入参===" + paramStr); PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL (url); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); // Open the connection with the URL conn.setRequestProperty("accept", "*/*"); // Set the general request property conn.setRequestProperty("Content- Type", "application/json" ); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); conn.setDoOutput( true ); // The following two lines must be set to send a POST request conn.setDoInput( true ); // Get the output stream corresponding to the URLConnection object out = new PrintWriter( new OutputStreamWriter(conn.getOutputStream(), "UTF- 8")); // Send request parameters out.print(paramStr); // flush the buffer of the output stream out.flush(); in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); // Define BufferedReader input stream to read URL response String line; while ((line = in.readLine()) != null ) { result += line; } } catch (Exception e) { System.out.println( "An exception occurred when sending POST request!" + e); throw e; } finally { // Use finally block to close output stream, input stream try { if (in != null ) in.close(); } catch (IOException ex) { } if (out != null) out.close(); } return result; }