POST请求从postman工作,但不从java代码工作
我在java中使用HttpUrlConnection发出post请求,我在postman上得到响应,但在java代码中调用时没有得到响应 下面是我的代码:POST请求从postman工作,但不从java代码工作,java,post,Java,Post,我在java中使用HttpUrlConnection发出post请求,我在postman上得到响应,但在java代码中调用时没有得到响应 下面是我的代码: public static JSONObject sendPostRequest(String requestURL, Map<String, String> params,Map<String,String>headers) throws IOException { S
public static JSONObject sendPostRequest(String requestURL,
Map<String, String> params,Map<String,String>headers) throws IOException {
StringBuffer response = new StringBuffer();
URL url = new URL(requestURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
StringBuffer requestParams = new StringBuffer();
if (params != null && params.size() > 0) {
for (Map.Entry<String,String> entry : headers.entrySet())
httpConn.setRequestProperty(entry.getKey(), entry.getValue());
httpConn.setDoOutput(true);
// creates the params string, encode them using URLEncoder
Iterator<String> paramIterator = params.keySet().iterator();
while (paramIterator.hasNext()) {
String key = paramIterator.next();
String value = params.get(key);
requestParams.append(URLEncoder.encode(key, "UTF-8"));
requestParams.append("=").append(
URLEncoder.encode(value, "UTF-8"));
requestParams.append("&");
}
// sends POST data
OutputStreamWriter writer = new OutputStreamWriter(
httpConn.getOutputStream());
writer.write(requestParams.toString());
writer.flush();
int responseCode = httpConn.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
公共静态JSONObject sendPostRequest(字符串requestURL,
映射参数,映射头)引发IOException{
StringBuffer响应=新的StringBuffer();
URL=新URL(请求URL);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
httpConn.setRequestMethod(“POST”);
httpConn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
StringBuffer requestParams=新的StringBuffer();
if(params!=null&¶ms.size()>0){
for(Map.Entry:headers.entrySet())
httpConn.setRequestProperty(entry.getKey(),entry.getValue());
httpConn.setDoOutput(真);
//创建参数字符串,使用URLEncoder对其进行编码
迭代器paramIterator=params.keySet().Iterator();
while(parameterator.hasNext()){
String key=paramIterator.next();
字符串值=params.get(键);
append(URLEncoder.encode(键,“UTF-8”);
requestParams.append(“=”).append(
编码(值,“UTF-8”);
requestParams.append(“&”);
}
//发送POST数据
OutputStreamWriter writer=新的OutputStreamWriter(
httpConn.getOutputStream());
writer.write(requestParams.toString());
writer.flush();
int responseCode=httpConn.getResponseCode();
BufferedReader in=新的BufferedReader(
新的InputStreamReader(httpConn.getInputStream());
字符串输入线;
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//打印结果
System.out.println(response.toString());
}
返回新的JSONObject().parse(response.toString());
}
回复代码:200
响应字符串:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Error</title></head><body> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" > <!-- <tr> --> <!-- <td class="dialog_header" colspan="3"> --> <!-- <table cellpadding="0" cellspacing="0" border="0" width="100%"> --> <!-- <tr> --> <!-- <td /></td> --> <!-- <td style="font-size:15px;"><span >Internal Server Error</span></td> --> <!-- <td ></td> --> <!-- </tr> --> <!-- </table> --> <!-- </td> --> <!-- </tr> --> <tr> <td style="width: 20px;"></td> <td ><br /> The requested web page either has an error or does not exist.<br /><br /> Please contact your system administrator or customer support.<br /><br /> </td> <td style="width: 20px;"></td> </tr> </table> </body></html>
Error
请求的网页有错误或不存在。
请与您的系统管理员或管理员联系客户支持。
什么样的请求需要服务器?这是一个从quicksilver API兑换礼品卡的请求。我正在正确设置请求正文和请求标头内部服务器错误请求的网页有错误或不存在。
但同一请求正在PostMan上运行服务器需要哪些参数和标头?它返回500个内部服务器错误。你能给我们提供邮递员的要求吗?