【问题标题】:translating curl command into java/android将 curl 命令翻译成 java/android
【发布时间】:2011-05-23 05:26:30
【问题描述】:

谁能帮我弄清楚如何将以下 curl 命令转换为 Java 语法以在 Android 应用程序中使用?

curl 命令是:

curl -u 用户名:密码 -H "Content-Type:application/vnd.org.snia.cdmi.container" http://localhost:8080/user

curl -u 用户名:密码 -T /home/user1/a.jpg -H "Content-Type:image" http://localhost:8080/user/a.jpg

谢谢

【问题讨论】:

    标签: java android curl httprequest


    【解决方案1】:

    您可以在 Android 中使用 Apache HttpClient 来执行 HTTP 帖子。

    • HttpClient 代码 sn-p(未测试)

      public void postData(String url) {
          // Create a new HttpClient and Post Header
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(url);
      
      try {
          // Set the headers (your -H options)
          httpost.setHeader("Content-type", "application/json");
          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
          nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);
      
      } catch (Exception e) {
          // Exception handling
      } 
      

      }

    • 查看以下链接了解基本身份验证部分(您的 -u 选项)

    http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html

    • 为您的文件上传检查以下答案(您的 -T 选项)

    How to upload a file using Java HttpClient library working with PHP

    【讨论】:

    • 嗨 ddewaele,我使用 PUT 而不是 post 来上传文件。只是改变 HttpPut 是一样的吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多