JAVA get, post (body) 요청

33513 단어 20201331
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;

public class TOOL {
     
	
	public static void main(String avg[]) {
     
		System.out.println(
				"///\r
"
+ "____ ___.__ ________ \r
"
+ "\\ \\/ /|__|_____ ____ \\______ \\ _____ \r
"
+ " \\ / | |\\__ \\ / \\ | | \\ \\__ \\ \r
"
+ " / \\ | | / __ \\_| | \\ | ` \\ / __ \\_\r
"
+ "/___/\\ \\|__|(____ /|___| //_______ /(____ /\r
"
+ " \\_/ \\/ \\/ \\/ \\/ \r
"
+ "\r
"
+ "//"); } /** * URL GET * * @param url * URL * @param param * , name1=value1&name2=value2 。 * @return URL */ public static JSONObject sendGet(String urlNameString) { String result = ""; BufferedReader in = null; try { // String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // URL URLConnection connection = realUrl.openConnection(); // connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("contentType", "utf-8"); // connection.connect(); // Map<String, List<String>> map = connection.getHeaderFields(); // for (String key : map.keySet()) { // System.out.println(key + "--->" + map.get(key)); } // BufferedReader URL in = new BufferedReader(new InputStreamReader( connection.getInputStream(),"utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(" GET !" + e); e.printStackTrace(); } // finally finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } System.out.println("GET :"+urlNameString); System.out.println("GET :"+result); return JSONObject.fromObject(result); } /** * URL POST * * @param url * URL * @param param * , name1=value1&name2=value2 。 * @return */ public static JSONObject sendPost(String url, JSONObject param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // URL URLConnection conn = realUrl.openConnection(); // // Content-Type:application/json;charset=UTF-8 Accept:application/json, text/javascript, */*; q=0.01 conn.setRequestProperty("Content-Type", "aapplication/json"); conn.setRequestProperty("charset", "UTF-8"); conn.setRequestProperty("Accept", "application/json, text/javascript, */*"); conn.setRequestProperty("q", "0.01"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // POST conn.setDoOutput(true); conn.setDoInput(true); // URLConnection // out = new PrintWriter(conn.getOutputStream()); out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8")); // out.print(param.toString()); // flush out.flush(); // BufferedReader URL // in = new BufferedReader(new InputStreamReader(conn.getInputStream())); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(" POST !" + e); e.printStackTrace(); } // finally 、 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } System.out.println("POST :"+url); System.out.println("POST :"+param); System.out.println("POST :"+result); if(isJsonObject(result)) { return JSONObject.fromObject(result); }else { return JSONObject.fromObject("{\"errcode\":\"500\",\"msg\":\" JSON, :"+result+"\"}"); } } /** * json * @param content * @return */ public static boolean isJsonObject(String content) { // , StringUtils.isEmpty(), content " " ,JSONObject.parseObject , // , 。 content , JSON 。 if(content.trim()==""||content.trim().length()<=0){ return false; } try { JSONObject jsonStr = JSONObject.fromObject(content); return true; } catch (Exception e) { return false; } } }

좋은 웹페이지 즐겨찾기