dsnrixf6765 2018-08-17 17:54
浏览 55
已采纳

登录请求后的POST请求仍然要求获得许可

I am trying to send two PostForm requests to a specified URL(webpage), which one of them is "login" and another one is "create a product". The problem is the second request(create a product) runs apart from the first request(login). That is why although the login runs successfully, the server sends me a permission error("You must be logged in first to create a product.")

I thought, it's because of concurrency, thus I used channels and even time package as well, but none of them had worked. Is there any solution to handle it?

func login(c chan string) {
    resp, _ := http.PostForm("http://example.com/login",
        url.Values{"username": {"sth"}, "password": {"sth"}})
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    time.Sleep(5000 * time.Millisecond)
    fmt.Printf("%v", string(body), "
")
    c <- string(body)
}

func CreateProduct() {
    resp, _ := http.PostForm("http://example.com/product",
        url.Values{"name": {"sth"}})
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Printf("%v", string(body), "
")
}

func main() {
    c := make(chan string)
    go login(c)
    str := <-c
    if str != "" {
        CreateProduct()
    }
}
  • 写回答

1条回答 默认 最新

  • dongshu7162 2018-08-17 18:33
    关注

    You don't need or want channels for this.

    You need to send a cookie or an authorise header. Check what the docs for the service you're trying to use say sbout authentication.

    If you specify which service people might be able to help more, but as a general guideline - if an api it probably wants a token, if a web page it probably expects a cookie, but beware you may also need to get around csrf protection in that case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c