Java HttpURLConnection post set params 設定請求引數的三種方法 實踐總結
阿新 • • 發佈:2019-02-10
/**
* the first way to set params
* OutputStream
*/
byte[] bytesParams = paramsStr.getBytes();
// 傳送請求params引數
OutputStream outStream=connection.getOutputStream();
outStream.write(bytesParams);
outStream.flush();
/**
* the second way to set params
* PrintWriter
*/
PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
//PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));
// 傳送請求params引數
printWriter.write(paramsStr);
printWriter.flush();
/**
* the third way to set params
* OutputStreamWriter
*/
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8");
// 傳送請求params引數
out.write(paramsStr);
out.flush();
demo:
/**
* @param pathurl
* @param paramsStr
* @return
*/
private static String postUrlBackStr(String pathurl, String paramsStr) {
String backStr = "";
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(pathurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 設定請求的方法為"POST",預設是GET
connection.setRequestMethod("POST");
connection.setConnectTimeout(50000);
connection.setReadTimeout(50000);
// User-Agent IE11 的標識
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.3; Trident/7.0;rv:11.0)like Gecko");
connection.setRequestProperty("Accept-Language", "zh-CN");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
/**
* 當我們要獲取我們請求的http地址訪問的資料時就是使用connection.getInputStream().read()方式時我們就需要setDoInput(true),
根據api文件我們可知doInput預設就是為true。我們可以不用手動設定了,如果不需要讀取輸入流的話那就setDoInput(false)。
當我們要採用非get請求給一個http網路地址傳參 就是使用connection.getOutputStream().write() 方法時我們就需要setDoOutput(true), 預設是false
*/
// 設定是否從httpUrlConnection讀入,預設情況下是true;
connection.setDoInput(true);
// 設定是否向httpUrlConnection輸出,如果是post請求,引數要放在http正文內,因此需要設為true, 預設是false;
connection.setDoOutput(true);
connection.setUseCaches(false);
/**
* the first way to set params
* OutputStream
*/
/* byte[] bytesParams = paramsStr.getBytes();
// 傳送請求params引數
OutputStream outStream=connection.getOutputStream();
outStream.write(bytesParams);
outStream.flush();
*/
/**
* the second way to set params
* PrintWriter
*/
/* PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
//PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));
// 傳送請求params引數
printWriter.write(paramsStr);
printWriter.flush();*/
/**
* the third way to set params
* OutputStreamWriter
*/
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8");
// 傳送請求params引數
out.write(paramsStr);
out.flush();
connection.connect();//
int contentLength = connection.getContentLength();
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();//會隱式呼叫connect()
baos = new ByteArrayOutputStream();
int readLen;
byte[] bytes = new byte[1024];
while ((readLen = inputStream.read(bytes)) != -1) {
baos.write(bytes, 0, readLen);
}
backStr = baos.toString();
Log.i(TAG, "backStr:" + backStr);
} else {
Log.e(TAG, "請求失敗 code:" + connection.getResponseCode());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return backStr;
}