Python的HTTPConnection.request是:
HTTPConnection.request(method, url, body=None, headers={})
想要用Java實現相同的目的。
而Java的httpURLConnection.setRequestProperty只能發送Key-value的兩個string類型的參數。
method我知道可以用httpURLConnection.setRequestMethod。但
我應該怎麼用Java才能發送Python代碼中的headers?
Java中怎麼做才能更改url?
/**
* 網絡訪問,上傳附件 五個參數: 1、String url:指定表單提交的url地址 2、Map<String, String>
* map:將上傳控件之外的其他控件的數據信息存入map對象 3、String filePath:指定要上傳到服務器的文件的客戶端路徑
* 4、byte[] body_data:獲取到要上傳的文件的輸入流信息,通過ByteArrayOutputStream流轉成byte[]
* 5、String charset:設置字符集
*/
public static String doPostSubmitBody(String url, Map<String, String> map,
String filePath, byte[] body_data, String charset) {
// 設置三個常用字符串常量:換行、前綴、分界線(NEWLINE、PREFIX、BOUNDARY);
final String NEWLINE = "\r\n";
final String PREFIX = "--";
final String BOUNDARY = "#";// 取代---------------------------7df3a01e37070c
HttpURLConnection httpConn = null;
BufferedInputStream bis = null;
DataOutputStream dos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 實例化URL對象。調用URL有參構造方法,參數是一個url地址;
URL urlObj = new URL(url);
// 調用URL對象的openConnection()方法,創建HttpURLConnection對象;
httpConn = (HttpURLConnection) urlObj.openConnection();
// 調用HttpURLConnection對象setDoOutput(true)、setDoInput(true)、setRequestMethod("POST");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
// 設置Http請求頭信息;(Accept、Connection、Accept-Encoding、Cache-Control、Content-Type、User-Agent)
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
httpConn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
// 調用HttpURLConnection對象的connect()方法,建立與服務器的真實連接;
httpConn.connect();
// 調用HttpURLConnection對象的getOutputStream()方法構建輸出流對象;
dos = new DataOutputStream(httpConn.getOutputStream());
// 獲取表單中上傳控件之外的控件數據,寫入到輸出流對象(根據HttpWatch提示的流信息拼湊字符串);
if (map != null && !map.isEmpty()) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = map.get(key);
dos.writeBytes(PREFIX + BOUNDARY + NEWLINE);
dos.writeBytes("Content-Disposition: form-data; "
+ "name=\"" + key + "\"" + NEWLINE);
dos.writeBytes(NEWLINE);
dos.writeBytes(URLEncoder.encode(value.toString(), charset));
// 或者寫成:dos.write(value.toString().getBytes(charset));
dos.writeBytes(NEWLINE);
}
}
// 獲取表單中上傳控件的數據,寫入到輸出流對象(根據HttpWatch提示的流信息拼湊字符串);
if (body_data != null && body_data.length > 0) {
dos.writeBytes(PREFIX + BOUNDARY + NEWLINE);
String fileName = filePath.substring(filePath
.lastIndexOf(File.separatorChar) + 1);
dos.writeBytes("Content-Disposition: form-data; " + "name=\""
+ "uploadFile" + "\"" + "; filename=\"" + fileName
+ "\"" + NEWLINE);
dos.writeBytes(NEWLINE);
dos.write(body_data);
dos.writeBytes(NEWLINE);
}
dos.writeBytes(PREFIX + BOUNDARY + PREFIX + NEWLINE);
dos.flush();
// 調用HttpURLConnection對象的getInputStream()方法構建輸入流對象;
byte[] buffer = new byte[8 * 1024];
int c = 0;
// 調用HttpURLConnection對象的getResponseCode()獲取客戶端與服務器端的連接狀態碼。如果是200,則執行以下操作,否則返回null;
if (httpConn.getResponseCode() == 200) {
bis = new BufferedInputStream(httpConn.getInputStream());
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
}
// 將輸入流轉成字節數組,返回給客戶端。
return new String(baos.toByteArray(), charset);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
}
if (bis != null) {
bis.close();
}
if (baos != null) {
baos.close();
}
httpConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}