Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用curl:Android的Https post请求_Java_Android_Facebook_Curl_Https - Fatal编程技术网

Java 使用curl:Android的Https post请求

Java 使用curl:Android的Https post请求,java,android,facebook,curl,https,Java,Android,Facebook,Curl,Https,我正在尝试使用curl执行https post请求。当我执行这个请求时,我既没有得到任何响应,也没有收到任何错误或异常。非常感谢您的帮助或任何关于这里出现问题的线索。谢谢 curl命令行格式: curl -X POST \ -F 'image=@filename.png;type=image/png' \ -F 'svgz=@filename.svgz;type=image/svg+xml' \ -F 'json={ "text" : "Hello world!", "

我正在尝试使用curl执行https post请求。当我执行这个请求时,我既没有得到任何响应,也没有收到任何错误或异常。非常感谢您的帮助或任何关于这里出现问题的线索。谢谢

curl命令行格式:

    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
这是一个不受信任的网络,所以,为此,我在link中做了如下操作


经过一段艰难的时间,我找到了解决问题的办法。现在,使用
MultipartEntity
,我可以像下面这样向服务器发送数据

    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);
BufferedReader in=null;
StringBuffer sb=新的StringBuffer();
BufferedReader inPost=null;
试一试{
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(mURL);
httpost.setHeader(“接受”,“*/*”);
setHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
List nvps=new ArrayList();
添加(新的BasicNameValuePair(“testkey1”、“myvalue1”);
添加(新的BasicNameValuePair(“testkey2”、“myvalue2”);
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response=httpclient.execute(httpost);
HttpEntity=response.getEntity();
inPost=new BufferedReader(新的InputStreamReader(entity.getContent());
字符串linePost=“”;
字符串NLPOST=System.getProperty(“line.separator”);
而((linePost=inPost.readLine())!=null){
sb.追加(linePost+NLPOST);
}
inPost.close();
如果(实体!=null){
entity.consumercontent();
}
httpclient.getConnectionManager().shutdown();
还有更多关于这个链接。。

谢谢你的努力。我找到了解决问题的办法
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();
    }
}
    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);
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();
    BufferedReader inPost = null;


    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();


            HttpPost httpost = new HttpPost(mURL);

            httpost.setHeader("Accept","*/*");
            httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            List <NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("testkey1", "myvalue1"));
            nvps.add(new BasicNameValuePair("testkey2", "myvalue2"));


            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();

            inPost = new BufferedReader(new InputStreamReader(entity.getContent()));
            String linePost = "";
            String NLPOST = System.getProperty("line.separator");
            while ((linePost = inPost.readLine()) != null) {
                sb.append(linePost + NLPOST);
            }
            inPost.close();
            if (entity != null) {
                entity.consumeContent();
            }


        httpclient.getConnectionManager().shutdown();