1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| package main
import ( "bytes" "context" "crypto/md5" "encoding/hex" "encoding/json" "fmt" "io" "io/ioutil" "log" "mime/multipart" "net/http" "os" "path/filepath" "strings"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/glog" )
const ( loginUrl = "https://ld246.com/login"
uploadImgUrl = "https://ld246.com/upload/editor"
uploadFilePath = "C:/Users/biuaxia/Downloads/Snipaste_2021-09-07_15-03-20.png" )
type loginReqData struct { NameOrEmail string `json:"nameOrEmail"` UserPassword string `json:"userPassword"` Captcha string `json:"captcha"` }
type loginRespData struct { Msg string `json:"msg"` Code int `json:"code"` Goto string `json:"goto"` TokenName string `json:"tokenName"` Token string `json:"token"` }
func main() { data := getLoginRespData()
uploadImg(data) uploadPic(data) }
func uploadImg(data loginRespData) { m := make(map[string]string) m[data.TokenName] = data.Token
r, e := g.Client(). Cookie(m). Post(uploadImgUrl, "file[]=@file:"+uploadFilePath) if e != nil { glog.Error(e) } else { fmt.Println("uploadImg-resp:", string(r.ReadAll())) err := r.Close() if err != nil { panic(err) } } }
func uploadPic(data loginRespData) { m := make(map[string]string) m[data.TokenName] = data.Token
var ( buffer = bytes.NewBuffer(nil) writer = multipart.NewWriter(buffer) )
formFile, _ := writer.CreateFormFile("file[]", filepath.Base(uploadFilePath)) file, _ := os.Open(uploadFilePath) _, _ = io.Copy(formFile, file) defer func(file *os.File) { err := file.Close() if err != nil { panic(err) } }(file) defer func(writer *multipart.Writer) { err := writer.Close() if err != nil { panic(err) } }(writer)
req, _ := http.NewRequest("POST", uploadImgUrl, buffer) req.Header.Set("Content-Type", writer.FormDataContentType()) req = req.WithContext(context.Background()) req.Header.Set("Cookie", data.TokenName+"="+data.Token) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47")
resp, _ := http.DefaultClient.Do(req) readAll, _ := ioutil.ReadAll(resp.Body) fmt.Println("uploadPic-resp:", string(readAll)) }
func getLoginRespData() loginRespData { ld := loginReqData{ NameOrEmail: "你的账号", UserPassword: getPassword("你的账号"), Captcha: "", }
reqBody, _ := json.Marshal(ld) log.Println("请求内容:", string(reqBody))
resp, _ := http.Post(loginUrl, "application/json", strings.NewReader(string(reqBody))) defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { panic(err) } }(resp.Body)
body, _ := ioutil.ReadAll(resp.Body) log.Println("响应内容:", string(body))
var lrd loginRespData _ = json.Unmarshal(body, &lrd) return lrd }
func getPassword(password string) string { h := md5.New() h.Write([]byte(password)) return hex.EncodeToString(h.Sum(nil)) }
|