0

我不确定我能做些什么来查看我的 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);
4

1 回答 1

0

记住破折号.....

conn.setRequestProperty("ContentType", "application/xml");

本来应该

conn.setRequestProperty("Content-Type", "application/xml");
于 2014-08-11T17:13:50.657 回答