package com.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Http { /** * Basic class post method * @param requestUrl request address * @param requestXml push data string * @return String return data */ public static String sendPost02(String requestUrl, String requestXml) { StringBuilder ret = new StringBuilder(); HttpURLConnection httpCon; try { httpCon = (HttpURLConnection) (new URL(requestUrl)).openConnection(); httpCon.setConnectTimeout(1000 * 30);//Set the connection host timeout (unit: milliseconds) httpCon.setReadTimeout(1000 * 30);//Set the timeout for reading data from the host (unit: milliseconds) httpCon.setDoOutput(true);//Set whether to output to httpUrlConnection, because this is a post request, the parameters should be placed in the http body, so it needs to be set to true, by default it is false; httpCon.setDoInput(true);// Set whether to read in from httpUrlConnection, by default it is true; httpCon.setRequestMethod("POST");// Set the request method to "POST", the default is GET httpCon.setUseCaches(false);// Post requests cannot use caches httpCon.setInstanceFollowRedirects(true);//Set whether this connection automatically handles redirection. If set to true, the system will automatically process the redirection; if set to false, you need to analyze the new url from the http reply and reconnect by yourself. //setRequestProperty is mainly to set properties in the HttpURLConnection request header such as Cookie, User-Agent (browser type), etc. httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//application/x-www-form-urlencoded: Form data is encoded as name/value pairs. This is the standard encoding format. multipart/form-data: Form data is encoded as a message, and each control on the page corresponds to a part of the message. text/plain: The form data is encoded in plain text, without any controls or formatting characters. httpCon.connect(); DataOutputStream postOut = new DataOutputStream(httpCon.getOutputStream()); String postContent = requestXml; postOut.write(postContent.getBytes("utf-8")); postOut.flush(); postOut.close(); if (httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) { String readLine = null; BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpCon.getInputStream(), "UTF-8")); while ((readLine = responseReader.readLine()) != null) { ret.append(readLine); } responseReader.close(); } } catch (IOException e) { e.printStackTrace (); } return ret.toString(); } }
Native http interface transmission
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326118563&siteId=291194637
Ranking