如何将该Curl命令传输到Java http帖子:
卷曲-不安全-XPOST -H'授权:XXXXX'-F'名称=测试名称'-F'文本=测试内容123456''http://serverhost.com/api/index.php?action=send'
谢谢大家。
我已经尝试过,但是我认为可能有更好的方法。
URL url = new URL(reqUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("X-Requested-With", "Curl");
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Authorization", authorization);
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(reqBody);
out.flush();
out.close();
答案 0 :(得分:0)
谢谢大家。我已经解决了。代码示例失败了: URL url =新URL(urlStr);
conn =(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(“ POST”);
//高级api 如果(!TextUtils.isEmpty(authorization)){
conn.setRequestProperty("Authorization", authorization);
}
conn.setRequestProperty(“ Connection”,“保持活动”);
conn.setRequestProperty(“ User-Agent”,
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty(“ Content-Type”,
"multipart/form-data; boundary=" + BOUNDARY);
out = new DataOutputStream(conn.getOutputStream());
//文字 if(textMap!= null){
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY)
.append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\""
+ inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
logger.info(inputName + "," + inputValue);
}
out.write(strBuf.toString().getBytes());
}