简体   繁体   English

使用POST从php cURL接收Java Web Service中的变量?

[英]Recieve variables in java Web Service from php cURL using POST?

I'm recieving 'null' on my Params. 我在Params上收到“ null”。 So far i've found people using "PathParams", "FormParams" and "QueryParams" on java to get their variables from the request, however in my case, using post i dont have PathParams, and for some reason FormParams and QueryParams are returning null as if i had never sent them. 到目前为止,我发现人们在Java上使用“ PathParams”,“ FormParams”和“ QueryParams”从请求中获取变量,但是在我的情况下,使用post我没有PathParams,由于某种原因,FormParams和QueryParams返回了null好像我从未发送过它们。 So i'd like help to solve this. 所以我想帮助解决这个问题。

My php cURL POST request: 我的php cURL POST请求:

<?php
$url = "http://localhost:8080/TestRest/test/asd";

$params = "param1=val1&param2=val2";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>

My java Web Service: 我的Java Web服务:

@Path("/test")
public class Streams {

    @POST
    @Path("/asd")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes({"application/x-www-form-urlencoded"})
    public String addStream(@FormParam("param1") String param1, 
                        @FormParam("param2") String param2) {
    return "param1 = "+param1;
    }
}

For some reason param1 is null. 由于某些原因,param1为空。 What am i doing wrong? 我究竟做错了什么?

I am going out on a limb and saying your problem may stem from this: 我四肢冒昧地说您的问题可能源于此:

urlencode($params);

For instance: a=1&b=2&c=3 is perfectly valid. 例如: a=1&b=2&c=3是完全有效的。

urlencode('a=1&b=2&c=3') goes to this: a%3D1%26b%3D2%26c%3D3 and your webservice may not understand what it is your are passing. urlencode('a=1&b=2&c=3')转到此: a%3D1%26b%3D2%26c%3D3 ,您的网络服务可能不了解您正在传递的内容。 It is looking for a=...&b=...&c=... and you are not giving it any of that. 它正在寻找a=...&b=...&c=...而您没有给出任何。

Try leaving off the urlencode() and seeing if it works. 尝试放弃urlencode()并查看它是否有效。 If it does, and you still want to ensure content like this&this is encoded properly, then you will need to parse the url parameters, encode them, and then recombine them for the post data. 如果是这样,你还是要确保内容像this&this被正确编码,那么你就需要解析URL参数,编码它们,然后重新组合它们为POST数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM