<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                通常的業務需求都會有請求http服務的需求,包括數據爬蟲、接口服務請求等等,該工具能提供一個高效易用的http請求客戶端,支持 cookie 保持,能方便的實現模擬登陸請求,鏈式操作方便進行參數配置。 ## 快速開始 通過如下方式,能快速發送一個 GET 請求 ~~~ client:=httpclient.NewClient() resp,_:=client.RunGet("http://www.baidu.com") ~~~ ## POST 請求 通過如下方式,能快速發送一個 POST 請求 ~~~ client:=httpclient.NewClient() resp,_:=client.RunPost("http://www.baidu.com") ~~~ 通常情況下 POST 請求都會有請求參數,通過如下方式添加 POST 參數 ### 方法1 使用 `FormParams` 方法配置一個 `map[string]interface{}` 參數; 默認 Orange 會以 `application/x-www-form-urlencoded` 的方式發送 POST 請求; 通過 `WithJsonRequest` / `WithFormRequest` 方法可以修改請求頭中的文檔類型; ~~~ client:=httpclient.NewClient() resp,_:=client.FormParams(map[string]interface{}{ "auth":123, }).RunPost("http://127.0.0.1/test.php?s=1") // 以json的方式發送請求,會將post轉換成 {"auth":123} resp,_:=client.WithJsonRequest().FormParams(map[string]interface{}{ "auth":123, }).RunPost("http://127.0.0.1/test.php?s=1") // 以表單提交的方式發送請求 resp,_:=client.WithJsonRequest().FormParams(map[string]interface{}{ "auth":123, }).RunPost("http://127.0.0.1/test.php?s=1") ~~~ ### 方法2 一種更為靈活的方法來發送 POST 參數; ~~~ // 通過 ContentType 配置請求頭文檔類型 client:=httpclient.NewClient().ContentType("application/json") // 通過 WithBody 添加請求 Body 中的信息 resp,_:=client.WithBody(`{"auth":123}`).RunPost("http://127.0.0.1/test.php?s=1") ~~~ ## 更多配置 ### cookie 保持 通過 `WithCookie` 鏈式方法能像瀏覽器一樣保持 cookie 解決模擬登陸抓包 session/cookie 問題; 注意:每一次調用 `httpclient.NewClient()` 相當于新創建一個瀏覽器,cookie 保持需要使用同一個 `httpclient.NewClient()` 返回的對象進行操作。 ~~~ client:=httpclient.NewClient().WithCookie() resp,_: = client.RunGet("http://www.baidu.com") resp,_ = client.RunGet("http://www.baidu.com/login") ~~~ ### 設置超時時間 Orange 框架中默認配置的請求超時時間是 10s 可以通過 `SetTimeout` 鏈式操作進行配置; ~~~ // 設置 3s 的超時時間 client:=httpclient.NewClient().SetTimeout(3) resp,_:=client.RunGet("http://www.baidu.com") ~~~ ### 設置請求頭 通過 `Header` 鏈式操作配置請求頭信息; ~~~ client:=httpclient.NewClient().Header("auth", "123") resp,_:=client.RunGet("http://www.baidu.com") ~~~ ### 添加文件 有的時候我們需要向服務端發送一個文件數據,可以通過該方式時間,添加文件后必須使用 POST 請求才能生效,并且添加文件后請求頭中的 Content-Type 將會被設置程成`multipart/form-data`,同樣也是使用鏈式操作進行添加 ~~~ client:=httpclient.NewClient().Header("auth", "123") resp,_:=client.WithFile("file", "./storage/1.jpg"). RunPost("http://127.0.0.1:8088/upload") ~~~ ### 關閉KeepAlives 默認情況下客戶端請求都是開啟了KeepAlives支持的(同時需要服務端也開啟KeepAlives),如果需要將客戶端的KeepAlives關閉可以使用該方法。 ~~~ client:=httpclient.NewClient().DisableKeepAlives() resp,_:=client.RunGet("http://www.baidu.com") ~~~ ## 響應 Orange 請求接口返回的是一個響應結構體,里面包含原始響應數據,執行時間,等信息; ~~~ client:=httpclient.NewClient() resp,_:=client.RunGet("http://www.baidu.com") // 獲取原始 *http.Response fmt.Println(resp.BodyRaw) // 獲取響應內容字符類型 fmt.Println(resp.String()) // 獲取響應內容 byte 類型 fmt.Println(resp.Body) // 獲取 cookie 信息 fmt.Println(resp.CookieData) // 獲取請求執行時間 fmt.Println(resp.GetRequestTime()) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看