Https发布请求使用curl:Android [英] Https post request using curl : Android

查看:119
本文介绍了Https发布请求使用curl:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用curl执行https post请求。当我执行这个请求,我既没有得到任何响应,也没有任何错误或异常。帮助或任何线索,这里发生了什么错误是值得赞赏的。感谢。

I am trying to execute a https post request using curl. When I execute this request, I am neither getting any response nor any error or exception. Help or any clue about what's going wrong here is appreciated. Thanks.

curl命令行格式:

curl command line format :

    curl -X POST \
-F 'image=@filename.png;type=image/png' \
-F 'svgz=@filename.svgz;type=image/svg+xml' \
-F 'json={ 
    "text" : "Hello world!",
    "templateid" : "0010",
    "timestamp" : "1342683312", 
    "location" : [ 37.7793, -122.4192 ],
    "facebook" :
    {
        "id": "738124695",
        "access_token": "<VALID_USER_FACEBOOK_TOKEN_WITH_PUBLISH_ACTIONS_PERMISSIONS",
        "expiration_date": "1342683312"                
    }
};type=application/json' \
https://sample.com/api/posts

Facebook发布代码:

Facebook posting code :

public static void uploadToFB() {
    HttpClient client = getNewHttpClient();
    HttpPost httpost = new HttpPost("https://sample.com/api/posts");
    httpost.addHeader("image", "filename.png");
    httpost.addHeader("svgz", "filename.svgz");
    httpost.addHeader("type", "application/json");
    httpost.setHeader("Content-type", "application/json");
    JSONObject data = new JSONObject();
    JSONObject facebook = new JSONObject();
    JSONArray location = new JSONArray();
    HttpResponse response = null;
    try {
        data.put("text","Hello world!");
        data.put("templateid","0010");
        data.put("timestamp","2012-07-08 09:00:45.312195368+00:00");

        location.put(37.7793);
        location.put( -122.4192);
        data.put("location", location);
        facebook.put("id", "738124695");
        facebook.put("access_token", "AAADdF92joPABAKmRojBuXZAZAP"+
            "qF8ZAxM2bM"+
             "UnIErUSYZB85y5vIHAZDZD");
        facebook.put("expiration_date", "2013-07-07T 22:00:00Z");
        data.put("facebook", facebook);

        System.out.println(" ---- data ----- "+data);

        StringEntity stringEntity = new StringEntity(data.toString());
        httpost.setEntity(stringEntity);
        try {
            response = client.execute(httpost);
            System.out.println(" --- response --- "+response);
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
            if(entity != null) {
                // A Simple Response Read
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                System.out.println(" ---- result ---- "+result);

                // Closing the input stream will trigger connection release
                instream.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

这是一个不受信任的网络,在链接中执行了类似下面的操作。

It was an untrusted network, so, for that I did something like below as in this link.

private static HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}


推荐答案

一些困难的时候,我得到了我的问题的解决方案。现在,使用 MultipartEntity ,我可以像下面那样发送数据到服务器。

After some hard time I got solution for my problem. Now, using MultipartEntity, I am able to send data to server like below.

    HttpClient httpClient = getHttpClient();

    HttpPost httpost = new HttpPost("https://sample.com/api/posts");
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile1 = new FileBody(new File("file.png"), "image/png");
    mpEntity.addPart("image", cbFile1);

    ContentBody cbFile2 = new FileBody(new File("file.svg"), "image/svg+xml");
    mpEntity.addPart("svgz", cbFile2);

    ContentBody cbFile3 = new StringBody(getJsonData().toString(), "application/json", Charset.forName("UTF-8"));
    mpEntity.addPart("json", cbFile3);

    httpost.setEntity(mpEntity);

这篇关于Https发布请求使用curl:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆