简体   繁体   English

通过HTTP客户端编写curl等效Java代码时遇到的问题

[英]Facing issue while writing curl equivalent java code through HTTP Client

I have a CURL command and that is working fine when i executed it. 我有一个CURL命令,当我执行它时,它工作正常。 Below is the curl command . 下面是curl命令。 If i am trying to write the same curl command in java using HTTP client i am getting the HTTP 403. I am not able to understand why the same error I am not getting when i executed CURL but in code i am getting . 如果我试图使用HTTP客户端在Java中编写相同的curl命令,则会得到HTTP403。我无法理解为什么执行CURL时却没有得到相同的错误,但在得到的代码中却是如此。 Can someone please help me in fixing my code . 有人可以帮我修复我的代码吗?

curl -v -X POST "http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28" -d '{"providerType":"MOTOID","email":"1234@idmtest.com","password":""}'

Below is my Java Code. 下面是我的Java代码。

public static void main(String[] args) {

        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost("http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28");
        post.addHeader("User-Agent","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
        post.addHeader("Host","iapi-va3.svcmot.com");
        post.addHeader("Accept","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
        post.addHeader("Accept", "*/*");
        post.addHeader("Content-Length", "66");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        Header[] headerArray=post.getAllHeaders();
        for(Header h:headerArray)
        {
            System.out.println(h.getName()+"---"+h.getValue());
        }

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("providerType", "MOTOID"));
            nameValuePairs.add(new BasicNameValuePair("email", "1234@idmtest.com"));
            nameValuePairs.add(new BasicNameValuePair("password", ""));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = client.execute(post);
            System.out.println("\n"+response);
            //System.out.println(response.getStatusLine());

            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println("\n"+line);
            }
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

There is a differnece, that you are sending a json to server from the curl, but in your program you send it as parameters. 有一个区别,您是从curl向服务器发送json,但在程序中将其作为参数发送。

Obviously You need to serialise the params as json in your app before sending. 显然,在发送之前,您需要在应用程序中将参数序列化为json。

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

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