Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如果在服务端处理视频文件下载,如何跳过403的限制 #38

Closed
chovYena opened this issue Jul 2, 2024 · 4 comments
Closed

Comments

@chovYena
Copy link

chovYena commented Jul 2, 2024

我想实现视频下载的功能,发现直接请求视频地址的话会403

@wujunwei928
Copy link
Owner

设置下header中的 referer 和 host

@chovYena
Copy link
Author

chovYena commented Jul 3, 2024

🧑‍🎤 可以例举下 这个两个参数具体设置什么值吗

@wujunwei928
Copy link
Owner

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	mp4Url := "https://f.video.weibocdn.com/u0/xxx.mp4"

	client := &http.Client{}

	req, err := http.NewRequest("GET", mp4Url, nil)
	if err != nil {
		log.Fatalln(err)
	}

	// 根据自己需要下载的网站调整Referer值, 个别网站无需设置也可
	req.Header.Set("Referer", "https://weibo.com")

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

	fmt.Println(resp.StatusCode)
	if resp.StatusCode != 200 {
		log.Fatalf("status code not ok: %d\n", resp.StatusCode)
	}

	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}

	err = os.WriteFile("weibo_test.mp4", respBody, os.ModePerm)
	if err != nil {
		log.Fatalln(err)
	}

}

上面以下载微博视频举例, 其他类似

@chovYena
Copy link
Author

感谢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants