drxd54816 2019-07-15 13:57
浏览 38
已采纳

我不能通过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 2019-07-17 07:59
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 设计数字电路得到要求结果
  • ¥15 hfss 组天线阵列报错:The A and B vectors must be parallel to the XY plane,如何解决?
  • ¥30 ansible连接设备报错
  • ¥60 pmsampsize包取值问题
  • ¥15 波形合成电路的设计学习
  • ¥15 unity脚本挂不上
  • ¥15 数学建模数学建模需要
  • ¥20 Java Sound Api 调用Mixer.getLine偶现Line unsupported错误。
  • ¥15 使用cuda加速opencv运算但是报错AttributeError: module 'cv2.cuda' has no attribute 'getCudaEnabledDeviceCount'
  • ¥15 java输入输出异常