GO POST 访问URL

2021-06-10 09:21
256
0
package main

import (
   "fmt"
   "io/ioutil"
   "net/http"
   "os"
   "strings"
)

func main(){
   str:="https://www.baidu.com/v2/dataconfig/app/city/queryAreaInfoByIds"
   resp,e:=http.Post(str,"application/json",strings.NewReader("{\"ids\":[\"510100\",\"510104\"]}"))
   if e!=nil{
      fmt.Println(e)
      return
   }
   defer resp.Body.Close()
   
   excelFile,createErr:=os.Create("D:/1111/test2.txt")
   if createErr!=nil{
      return
   }
   body,readErr:=ioutil.ReadAll(resp.Body)
   s:=string(body)
   fmt.Println(s)
   if readErr!=nil{
      return
   }
   num,writeErr:=excelFile.Write(body)
   if writeErr!=nil{
      return
   }
   fmt.Println(num)
   if e:=excelFile.Close(); e!=nil{
      return
   }
}

全部评论