gogoWebsite

Java implementation sends get/post request and carries parameters

Updated to 18 days ago
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import org.; import org.; import .*; import ; import .*; /** * http request tool */ public class HttpUtil { // private static final Logger logger = (); /** * Send post request to carry json data * @param URL * @param json * @param headerMap * @return */ public static String sendPostJson(String URL, JSONObject json, HashMap<String,String> headerMap) { CloseableHttpClient client = (); HttpPost post = new HttpPost(URL); Iterator<<String, String>> iterator = ().iterator(); while (()) { <String, String> next = (); ((),()); } String result; try { StringEntity s = new StringEntity((), "utf-8"); (new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); (s); // Send a request HttpResponse httpResponse = (post); (().toString()); // Get the response input stream InputStream inStream = ().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line; while ((line = ()) != null) (line + "\n"); (); result = (); if (().getStatusCode() == HttpStatus.SC_OK) { ("The request server is successful, and the corresponding processing is performed"); } else { ("Requesting the server failed"); } } catch (Exception e) { throw new RuntimeException(e); } return result; } /** * @param url Access address * @param headerMap header parameter; you can convert the string type into map through the following tool class * @param contentMap requires the transmission of parameter parameters; the object can be converted into map through json * @return Return the data returned by the web page */ public static String sendPostForm(String url, Map<String, String> headerMap, Map<String, String> contentMap) { String result = null; CloseableHttpClient httpClient = (); HttpPost post = new HttpPost(url); // Set request header // ("Content-Type", "application/x-www-form-urlencoded"); List<NameValuePair> content = new ArrayList<NameValuePair>(); //Generate content to entity Iterator iterator = ().iterator(); while (()) { <String, String> elem = (<String, String>) (); (new BasicNameValuePair((), ())); } CloseableHttpResponse response = null; try { //Loop to increase header Iterator headerIterator = ().iterator(); while (()) { <String, String> elem = (<String, String>) (); ((), ()); } if (() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(content, "UTF-8"); (entity); } //Send a request and receive return data response = (post); if (response != null && ().getStatusCode() == 200) { //Get the body part of the response HttpEntity entity = (); //Read the body part of the reponse and convert it into a string result = (entity); } return result; } catch (UnsupportedEncodingException e) { (); } catch (ClientProtocolException e) { (); } catch (IOException e) { (); } finally { try { (); if (response != null) { (); } } catch (IOException e) { (); } } return null; } /** * Send a get request * @param url Request URL * @param param Request parameters key:value url carries parameters or does not fill in without parameters * @return */ public static String doGet(String url, Map<String, String> param) { // Create Httpclient object CloseableHttpClient httpclient = (); String resultString = ""; CloseableHttpResponse response = null; try { // Create uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : ()) { (key, (key)); } } URI uri = (); // Create http GET request HttpGet httpGet = new HttpGet(uri); // Execute the request response = (httpGet); // Determine whether the return status is 200 if (().getStatusCode() == 200) { resultString = ((), "UTF-8"); } } catch (Exception e) { (); } finally { try { if (response != null) { (); } (); } catch (IOException e) { (); } } return resultString; } }