美文网首页
http 请求get / post

http 请求get / post

作者: 柯蓝_e007 | 来源:发表于2019-05-07 09:30 被阅读0次
    func httpGet(url string) (ret string){
    
        resp, err := http.Get(url)
        if err != nil {
            // handle error
        }
        defer func() {
            if(resp == nil){
                return
            }else if(resp.Body == nil){
                return
            }
            resp.Body.Close()
        }()
        if(resp == nil || resp.Body == nil){
            return ""
        }
        body, err := ioutil.ReadAll(resp.Body)
        if(err != nil || body == nil ){
            log.Println("httpGet error : ",err.Error()," body = ",body)
            return ""
        }
        return string(body)
    }
    
    func httpPost(requestUrl string, paramMap map[string]string) (ret string) {
    
        params := url.Values{}
    
        for key, value := range paramMap {
            params.Add(key, value)
        }
        resp, _ :=  http.PostForm(requestUrl,params);
    
        defer func() {
            if(resp == nil){
                return
            }else if(resp.Body == nil){
                return
            }
            resp.Body.Close()
        }()
        if(resp == nil || resp.Body == nil){
            return ""
        }
        body,err:= ioutil.ReadAll(resp.Body)
        if(err != nil || body == nil){
            log.Println("httpPost error : ",err.Error()," body = ",body)
            return ""
        }
        return  string(body)
    
    }
    

    相关文章

      网友评论

          本文标题:http 请求get / post

          本文链接:https://www.haomeiwen.com/subject/hpusoqtx.html