import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; public class HttpPostUtil { public void static test(){ try { //設置客戶端編碼 // Create HttpClient Object HttpClient httpClient = HttpClients.createDefault(); // Post請求 HttpPost httppost = new HttpPost("http://notify.dev.fenqile.com"); //設置post編碼 httppost.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8); httppost.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8); httppost.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8); httppost.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8); //設置post編碼 // 設置參數 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("appid", "xxxxx")); httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //設置報文頭 httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); // 發送請求 HttpResponse httpresponse = httpClient.execute(httppost); // 獲取返回數據 HttpEntity entity = httpresponse.getEntity(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); entity.writeTo(outputStream); System.out.println(new String(outputStream.toByteArray(), "utf-8")); if (entity != null) { entity.consumeContent(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { HttpPostUtil.test(); } }