简体   繁体   中英

I can not send a post request with 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???

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM