drxd54816
drxd54816
2019-07-15 13:57

我不能通过Golang发送发帖请求吗?

已采纳

VBA code works well

Set oFields = CreateObject("Scripting.Dictionary")
With oFields
    .Add "dor_user", "51"
    .Add "login", "nvivc"
    .Add "pass", "51256"
End With
For Each sName In oFields
    oFields(sName) = sName & "=" & EncodeUriComponent(oFields(sName))
Next
sPayLoad = Join(oFields.Items(), "&")
With CreateObject("MSXML2.XMLHTTP")
    .Open "POST", "http://kasant.gvc.oao.rzd:8888/kasant/login?", False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .setRequestHeader "Content-Length", LenB(sPayLoad)
    .Send (sPayLoad)
    Do While .readyState <> 4
        DoEvents
    Loop
End With

Login successful FIDDLER

POST http://kasant.gvc.oao.rzd:8888/kasant/login? HTTP/1.1

Accept: /

Content-Type: application/x-www-form-urlencoded

Accept-Language: ru,en-US;q=0.7,en;q=0.3

UA-CPU: AMD64

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Win64; x64; Trident/7.0; .NET4.0C; .NET4.0E)

Host: kasant.gvc.oao.rzd:8888

Content-Length: 34

Connection: Keep-Alive

Pragma: no-cache

Cookie: dor_user=""; kasant_pass=""; kasant_user=""; kasant_pass_save=false; JSESSIONID=0000VSeSKuzaru0vpwzeeSQF29Y:1ak0n0hlm

dor_user=51&login=nvivc&pass=51256

JSESSIONID=0000VSeSKuzaru0vpwzeeSQF29Y:1ak0n0hlm

Code Golang

urlLogin := "http://kasant.gvc.oao.rzd:8888/kasant/login?"
formData := url.Values{
    "dor_user": {"51"},
    "login":    {"nvivc"},
    "pass":     {"51256"},
}
client := &http.Client{}
req, _ := http.NewRequest("POST", urlLogin, bytes.NewBufferString(formData.Encode()))
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E)")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, _ := client.Do(req)
defer resp.Body.Close()

FIDDLER

POST http://kasant.gvc.oao.rzd:8888/kasant/login? HTTP/1.1 Host:

kasant.gvc.oao.rzd:8888 User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;

Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E)

Content-Length: 34 Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip

dor_user=51&login=nvivc&pass=51256

Missing cookies and JSESSIONID. Login failed!!! Tell me what is the error???

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsa99349 dsa99349 2年前

    Thanks @Peter

    urlLogin := "http://kasant.gvc.oao.rzd:8888/kasant/login?"
    formData := url.Values{
        "dor_user": {"51"},
        "login":    {"nvivc"},
        "pass":     {"51256"},
    }
    cookieJar, _ := cookiejar.New(nil)
    client := &http.Client{
        Jar: cookieJar,
    }
    respp, _ := client.Post(urlLogin, "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode()))
    defer respp.Body.Close()
    
    点赞 评论 复制链接分享

相关推荐