【发布时间】:2014-07-28 16:01:22
【问题描述】:
我不确定我能做些什么来查看我的 POST 请求有什么问题。我使用 CURL 库在 PHP 中制作了一个快速原型来构建我的请求。当我尝试在 JSP 页面中执行相同的功能时,我收到 HTTP 错误 415。我不确定如何修复它。我在 PHP 页面和 Web 调试器(提琴手)上都得到了正确的结果,没有错误。我还能够使用我正在使用的 REST api 的另一部分在此 JSP 页面上成功执行获取请求。我该怎么做才能找到问题?
这是JSP代码
String xmlRequest = "<viewParameters><vslText>[field1] = '01510486'</vslText></viewParameters>";
try
{
URL url = new URL("*****");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//Connection Properties
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setFollowRedirects(true);
//Header Parameters
conn.addRequestProperty("X-IntegrationServer-Username", "******");
conn.addRequestProperty("X-IntegrationServer-Password", "*******");
conn.setRequestProperty("ContentType", "application/xml");
conn.setRequestProperty("Host", "*********");
conn.setRequestProperty("Content", "viewParameters");
conn.setRequestProperty("Accept", "application/xml");
//Post Parameter
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(xmlRequest);
conn.connect();
//CHECK HTML RESPONSE
out.print("<br>Response code = " + conn.getResponseCode());
out.print("<br>Response message = " + conn.getResponseMessage() + "<br><br>");
//Output Here
InputStream input = conn.getInputStream();
//Takes XML and puts it into one string variable
BufferedReader inputReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String inline = "";
while ((inline = inputReader.readLine()) != null) {
sb.append(inline);
}
inputReader.close();
out.print(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
这是有效的 PHP 代码
<?php
$curl = curl_init();
//xml to send to server
$xmlRequest = "<viewParameters><vslText>[field1] = '********'</vslText></viewParameters>";
//took out 'Content-Length: ' . strlen($xmlRequest)
$requestParam = array('X-IntegrationServer-Username: ********', 'X-IntegrationServer-Password: ************', 'Host: *************', 'Content-Type: application/xml', 'Content: viewParameters');
curl_setopt($curl, CURLOPT_HTTPHEADER, $requestParam);
curl_setopt($curl , CURLOPT_URL, '***************');
//stops SSL errors but also turns off security for it check this link http://stackoverflow.com/questions/21187946/60-ssl-certificate-self-signed-certificate-in-certificate-chainbool
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
//if no response comes through tries to give error message
if(!$response){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
curl_close($curl);
$xml = simplexml_load_string($response);
【问题讨论】:
-
415 error = 服务器拒绝为请求提供服务,因为请求实体的格式不受请求方法的请求资源支持。 所以我猜您错过了转化中的某些内容
-
您的 jsp 代码中的
xmlRequest定义在哪里? -
您查看过回复信息吗?应该有一条消息与错误 415 一起出现。415 是不受支持的媒体类型。您是否将任何文件上传到服务器?
-
@RiggsFolly 我不知道我真正需要在哪里转换任何东西。也许这是我不需要用 CURL 库做的事情,但我没有用 php。
-
@Jorge Campos 抱歉,它就在 try 块之前,我将编辑显示。
标签: php jsp http post http-status-code-415