0
0

More than 1 year has passed since last update.

Go言語でhttpリクエスト結果が403の解決方法の一例

Posted at

はじめに

カクヨムのサイトをGo言語のnet/httpのライブラリーを使用していてデータ取得していたけど
ある時から、403と表示するようになった。curlコマンドでは取得できるのでなんでかわからなかったけど
可決方法が分かったのでやり方を記載する。

失敗時のやり方

以下のプログラムを組んだ時に403と返す。

main.go
package main

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

func main() {
    url = "https://kakuyomu.jp/"
    web1(url)
}

func web1(url string) {

    resp, _ := http.Get(url)
    defer resp.Body.Close()

    byteArray, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(byteArray))
}

実行すると以下の通りになる。

log
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

解決の方法

以下の通りに、ヘッダーにAcceptとUser-Agent情報を追加するとうまくいく

main.go
package main

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

func main() {
    url = "https://kakuyomu.jp/"
    web2(url)
}

func web2(url string) {

    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        return
    }
    req.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`)
    req.Header.Add("User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`)
    client := new(http.Client)

    resp, err := client.Do(req)
    defer resp.Body.Close()
    if err != nil {
        return
    }

    byteArray, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(byteArray))
}

実行すると取得できるようになる。

まとめ

とりあえず、http.Get関数を使用して失敗しても、ヘッダーに情報取得することでできることが分かったのでとりあえずこのまま進める

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0