2013-01-14 132 views
4

我正在嘗試使用curl來執行https發佈請求。當我執行此請求時,我既沒有收到任何迴應,也沒有發現任何錯誤或異常。幫助或任何有關錯誤的線索是值得讚賞的。謝謝。使用curl的https發佈請求:Android

curl命令行格式:

curl -X POST \ 
-F '[email protected];type=image/png' \ 
-F '[email protected];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的郵政代碼:

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(); 
    } 
} 

這是不信任的網絡,所以,對於我不喜歡的東西下面爲this鏈接。

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(); 
    } 
} 

回答

2

經過一段時間的困難,我得到了解決方案,我的問題。現在,使用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); 
3
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(); 

也較爲此鏈接.. http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

+0

感謝ü爲您efffort。我解決了我的問題 – Braj