directory
To obtain
Request gets params data
Request Obtaining raw data
Rely on
request
Send the POST Params call
Send post params
Send the POST RAW call
Send post raw
Rely on
Pom depends on
code:
Download.csdn.net/download/we…
To obtain
Request gets params data
@RequestMapping(value = "/getTest", produces = { "application/xhtml+xml; charset=UTF-8" }) public String getTest(HttpServletRequest req) { String params = req.getParameter("params") == null ? "" : req.getParameter("params"); System.out.println(params); return params; }Copy the code
Request Obtaining raw data
@RequestMapping(value = "/getTest1", produces = { "application/xhtml+xml; charset=UTF-8" }) public String getTest1(HttpServletRequest req) { String params = readRaw(req); System.out.println(params); return params; } public static String readRaw(HttpServletRequest Request) {String result = ""; try { InputStream inputStream = request.getInputStream(); ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) ! = -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inputStream.close(); result = new String(outSteam.toByteArray(), "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; }Copy the code
Rely on
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Copy the code
request
Send the POST Params call
Keyval params form Map<String, Object> mp = new HashMap<String, Object>(); mp.put("params", 123); String sr1 = HttpRequestUitlTest.sendPostParams("http://localhost:9996/getTest1", mp); System.out.println(sr1);Copy the code
Send post params
/** * @param url * @param params ** @return */ public static String sendPostParams(String url, Map params) { BufferedReader in = null; Try {// define HttpClient HttpClient client = new DefaultHttpClient(); // Instantiate the HTTP method HttpPost request = new HttpPost(); request.setURI(new URI(url)); NVPS = new ArrayList<NameValuePair>(); for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String value = String.valueOf(params.get(name)); nvps.add(new BasicNameValuePair(name, value)); // System.out.println(name +"-"+value); } request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = client.execute(request); int code = response.getStatusLine().getStatusCode(); If (code == 200) {// Request success in = new InputStreamReader(new InputStreamReader(response.getentity ().getContent(), "UTF-8 ")); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) ! = null) { sb.append(line + NL); } in.close(); return sb.toString(); } else {// system.out.println (" status code: "+ code); return null; } } catch (Exception e) { e.printStackTrace(); return null; }}Copy the code
Send the POST RAW call
JSONObject jsonObject = new JSONObject(); jsonObject.put("params", "{'chaincodeID':{'name':'fscs'},'ctorMsg': {'args':['query','Bob']}}"); jsonObject.put("id", 5); String transJson = jsonObject.toString(); / / send POST httclient request raw String sr2. = HttpRequestUitlTest sendPostRaw (" http://localhost:9996/getTest1 ", transJson); System.out.println(sr2);Copy the code
Send post raw
/** ** Sends a POST request to the specified URL ** @param URL Indicates the URL to send the request * @param param ** @return indicates the response result of the remote resource ** / public static String sendPostRaw(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); Conn. SetRequestProperty (" the user-agent ", "Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // To send a POST request, the following two lines must be set: conn.setdoOutput (true); conn.setDoInput(true); Out = new PrintWriter(conn.getOutputStream()); Out.print (param); // Flush the buffer of the output stream out.flush(); In = new BufferedReader(new InputStreamReader(conn.getinputStream ())); String line; while ((line = in.readLine()) ! = null) { result += line; }} catch (Exception e) {system.out.println (" error sending POST request! + e); e.printStackTrace(); } // Use finally blocks to close output streams, input streams finally {try {if (out! = null) { out.close(); } if (in ! = null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }Copy the code
Rely on
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
Copy the code
Pom depends on
< the dependency > < groupId > org, apache httpcomponents < / groupId > < artifactId > httpmime < / artifactId > < version > 4.5.2 < / version > </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> The < version > 4.5.8 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> < the groupId > Commons httpclient - < / groupId > < artifactId > Commons httpclient - < / artifactId > < version > 3.1 < / version > < / dependency > < the dependency > < groupId > org, apache httpcomponents < / groupId > < artifactId > httpclient < / artifactId > < version > 4.5.3 < / version > </dependency>Copy the code
ok
Continuously updated