[英]translating curl command into java/android
有人可以幫我弄清楚如何將以下curl命令轉換為Java語法,以便在Android應用程序中使用嗎?
curl命令是:
curl -u username:password -H“Content-Type:application / vnd.org.snia.cdmi.container” http:// localhost:8080 / user
curl -u username:password -T /home/user1/a.jpg -H“Content-Type:image” http:// localhost:8080 / user / a.jpg
謝謝
您可以在Android中使用Apache HttpClient來執行HTTP帖子。
HttpClient代碼段(未經測試)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.