目前正在尝试创建一个分段上传请求来上传一个 png 以及一些额外的正文参数。服务器正在返回响应:
请求正文不包含指定的字节数。得到 142,827,预期 146,836
所以很明显,计算出了点问题。我一生都无法弄清楚如何实际手动计算正文的内容长度并为请求正确归档。请参阅下面我发送请求的代码:
func signUp(email, username, password, profilePath string) account {
session := createClient()
capToke := captchaToken(session)
solved := solveCaptcha(capToke, "example.com")
if solved == failed {
panic("Solve Failed")
}
extraParams := map[string]string{
"Username": username,
"Age": string(random(18, 21)),
"Gender": "1",
"EmailAddress": email,
"Password": password,
"ConfirmPassword": password,
"g-recaptcha-response": solved,
"Recaptcha": "",
"ReceiveNewsletter": "false",
}
req, err := makeFileUploadRequest("https://example.com", extraParams, "ProfileImage", profilePath)
if err != nil {
panic(err)
}
req.Header.Set("Host", "example.com")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
req.Header.Set("Referer", "https://example.com")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Upgrade-Insecure-Requests", "1")
client := createClient()
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
panic(err)
} else {
var bodyContent []byte
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
resp.Body.Read(bodyContent)
resp.Body.Close()
fmt.Println(bodyContent)
}
var acc account
acc.email = email
acc.password = password
acc.username = username
return acc
}
func makeFileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
fi, err := file.Stat()
if err != nil {
return nil, err
}
file.Close()
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil {
return nil, err
}
ht, _ := http.NewRequest("POST", uri, body)
ht.Header.Set("Content-Type", writer.FormDataContentType())
writer.Close()
return ht, err
}