Python的HTTPConnection.request需要四个参数,Java怎么写?
问题描述:
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;
}
答
String path = "http://m.xylbh.cn/android.xml";
try {
URL url = new URL(path);
//建立连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式
//conn.setRequestMethod(GET);
//设置请求超时时间
conn.setConnectTimeout(5000);
//设置读取超时时间
conn.setReadTimeout(5000);
//判断是否获取成功
//获得输入流
InputStream is = conn.getInputStream();