zoukankan      html  css  js  c++  java
  • go语言模拟post请求----go语言学习笔记(一)

    go模拟json格式数据请求方式:

    package main

    import (
    "net/http"
    "fmt"
    "io/ioutil"
    "bytes")
      func main(){
      url := "http://www.baidu.com/" //你要请求的url地址
      cx_json :={"user":"aaa","usernam":"aaaaaa"} //传递的json格式的参数,有时需要格式化
    var jsonStr = []byte(cx_json)
      req,err :=http.NewRequest("POST",url,bytes.NewBuffer(jsonStr))

      req.Header.Set("Content-Type", "application/json")
        client :=&http.Client{}
        resp,err :=client.Do(req)
    
        if err !=nil{
            panic(err)
        }
    
        defer resp.Body.Close()
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println("response Body:", string(body))
    }

    go 模拟form表单的post请求:

    import (
        "fmt"
        "net/http"
        "net/url"
        "io/ioutil"
        "encoding/json"
        "time"
    )
    
    
    func main(){
        postValue := url.Values{
            "username": {"aaaa"},
            "user":{"aaaa"},
        }
        resp,err :=http.PostForm("http://www.baidu.com",postValue)
        resp.Header.Add("Content-Type", "application/x-www-form-urlencoded")
        if err != nil{
            fmt.Println(err)
        }
        defer resp.Body.Close()
        body,err := ioutil.ReadAll(resp.Body)
        if err != nil{
    panic(err)}
        defer resp.Body.Close()
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println("response Body:", string(body))
     }}
  • 相关阅读:
    Django forms组件
    Django 分页器
    Django Ajax
    Django 多表操作2
    js12种应该注意的地方
    Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试
    python学习-文件操作
    关于redis搭建环境
    扩展知识
    javascript之Banner图片焦点轮播
  • 原文地址:https://www.cnblogs.com/jinjidedale/p/8257252.html
Copyright © 2011-2022 走看看