Detailed explanation of Java sending post method
- 2021-07-18 07:57:12
- OfStack
Summarize the method of sending post by java using http under 1:
1. The post request is used to send parameters in json format:
/**
* post Request (used to request json Format parameters)
*
* @param url Address
* @param params json Parameters of the format
* @return
*/
public static String doPost(String url, String params) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost( url );// Create httpPost
httpPost.setHeader( "Accept", "application/json" );
httpPost.setHeader( "Content-Type", "application/json" );
String charSet = "UTF-8";
StringEntity entity = new StringEntity( params, charSet );
httpPost.setEntity( entity );
CloseableHttpResponse response = null;
try {
response = httpclient.execute( httpPost );
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString( responseEntity );
return jsonString;
} else {
logger.error( " Request return :" + state + "(" + url + ")" );
}
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
2. Parameters for sending key-value format
/**
* post Request ( Used for key-value Parameters of the format )
*
* @param url
* @param params
* @return
*/
public static String doPost(String url, Map params) {
BufferedReader in = null;
try {
// Definition HttpClient
HttpClient client = new DefaultHttpClient();
// Instantiation HTTP Method
HttpPost request = new HttpPost();
request.setURI( new URI( url ) );
// Setting parameters
List<NameValuePair> 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 succeeded
in = new BufferedReader( 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;
}
}
Third, send an get request
/**
* get Request
*
* @return
*/
public static String doGet(String url) {
try {
HttpClient client = new DefaultHttpClient();
// Send get Request
HttpGet request = new HttpGet( url );
HttpResponse response = client.execute( request );
/** The request was sent successfully and received a response **/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/** Read the returned from the server json String data **/
String strResult = EntityUtils.toString( response.getEntity() );
return strResult;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}