顯示具有 HTTP 標籤的文章。 顯示所有文章
顯示具有 HTTP 標籤的文章。 顯示所有文章

2021年10月18日 星期一

curl 常用語法

 

參考:

https://blog.techbridge.cc/2019/02/01/linux-curl-command-tutorial/

https://www.itread01.com/p/190409.html


應用範例:

curl -X POST -i -H "Content-Type: application/json;charset=utf-8" -d '{"client_id": "test","client_secret": "test"}' "https://localhost:8080/auth"


1.未指定任何參數,預設使用 GET 連線:

curl http://localhost:8080/test.jsp

2.指定使用 POST:

curl -X POST  http://localhost:8080/test.jsp

3.印出 Response Status 資訊:

curl -X POST -i http://localhost:8080/test.jsp

4.指定 Request Header:

curl -H "Content-Type: application/json;charset=utf-8"

5.指定 RequestBody為 JSON(注意JSON內容若使用雙引號,則完成JSON要使用單引號框住):

curl -X POST  -H "Content-Type: application/json;charset=utf-8" -d '{"client_id": "test","client_secret": "test"}' 

6.指定請求RequestBody來源為檔案(檔案路徑前加@):

curl -X POST  -H "Content-Type: application/json;charset=utf-8" -d '@reqt-data.json'

7..指定 HTTP POST  FROM Data:

curl -X POST --data "email=test@example.com&press=%20OK%20"  http://www.example.com/form.php

8.指定使用 cookie:

curl --cookie "name=Jack" http://www.example.com

9.指定 client 公鑰憑證路徑及私鑰 key檔案(應用於 two-way SSL)

curl -E ./client.crt --key client-private.key 'https://test-site/page'

10.連線 HTTPS 並預設信任

curl https://a-site.com/page --insecure





2010年3月3日 星期三

利用 Servlet 下載檔案儲存(不以應用程式開啟)

//於 Servlet 設定 ContentType 與 Header,讓瀏覽器知道不要以應用程式開啟 
// Set the headers.
res.setContentType("application/x-download");
res.setHeader("Content-Disposition", "attachment; filename=" + filename);

// Send the file.
OutputStream out = res.getOutputStream(  );
returnFile(filename, out);  



//returnFile 實作內容
public static void returnFile(String filename, OutputStream out)
                             throws FileNotFoundException, IOException {
    InputStream in = null;
    try {
      in = new BufferedInputStream(new FileInputStream(filename));
      byte[  ] buf = new byte[4 * 1024];  // 4K buffer
      int bytesRead;
      while ((bytesRead = in.read(buf)) != -1) {
        out.write(buf, 0, bytesRead);
      }
    }
    finally {
      if (in != null) in.close(  );
    }
  } 
 
參考網址:
http://onjava.com/pub/a/onjava/excerpt/jebp_3/index1.html?page=2
http://onjava.com/pub/a/onjava/excerpt/jebp_3/index3.html 

curl 常用語法

  參考: https://blog.techbridge.cc/2019/02/01/linux-curl-command-tutorial/ https://www.itread01.com/p/190409.html 應用範例: curl -X POST -i -H ...