dongshan2004 2019-03-04 14:40
浏览 53
已采纳

Golang“登录到站点并下载xls文件”? [关闭]

Tell me how to use Golang to login to the site. Download xls file is obtained, but in order to have data in the Excel table, you need to log in to the site. The site is located on the company's server. If you can tell how to do it.

For example, the VBA code that I use to do this.

Set oFields = CreateObject("Scripting.Dictionary")
With oFields
    .Add "login", "sdiscor"
    .Add "password", "sdiscor"
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://effect.gvc.oao.rzd/cgi_bin/effect_access.pl?", False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .setRequestHeader "Content-Length", LenB(sPayLoad)
    .Send (sPayLoad)

    Do While .readyState <> 4
        DoEvents
    Loop

    webLink = "http://effect.gvc.oao.rzd/effect/table/***&LOGIN=&PASSWORD="
    vLocalFile = ThisWorkbook.Path & "\SIS-Effect.xls"

    .Open "GET", webLink, True
    .Send

    Do While .readyState <> 4
        DoEvents
    Loop

    oResp = .responseBody

    vFF = FreeFile
    If Dir(vLocalFile) <> "" Then Kill vLocalFile
    Open vLocalFile For Binary As #vFF
    Put #vFF, , oResp
    Close #vFF
End With

Thank leolara!!! Final code

func main() {
urlLogin := "http://effect.gvc.oao.rzd/cgi_bin/effect_access.pl?"
urlDownload := "http://effect.gvc.oao.rzd/effect/table/OZODO10U.XLS?DAT=2019.03.04&LOGIN=&PASSWORD="

cookieJar, _ := cookiejar.New(nil)

client := &http.Client{Jar: cookieJar,}

_, err := client.PostForm(urlLogin,
    url.Values{"login": {"sdiscor"}, "password": {"sdiscor"}})
if err != nil {
    fmt.Println("Error while downloading", urlLogin, "-", err)
    return
}

fileName := "1.xls"
fmt.Println("Downloading", urlDownload, "to", fileName)
output, err := os.Create(fileName)
if err != nil {
    fmt.Println("Error while creating", fileName, "-", err)
    return
}
defer output.Close()

resp, err := client.Get(urlDownload)
if err != nil {
    fmt.Println("Error while downloading", urlDownload, "-", err)
    return
}
defer resp.Body.Close()

n, err := io.Copy(output, resp.Body)
if err != nil {
    fmt.Println("Error while downloading", urlDownload, "-", err)
    return
}

fmt.Println(n, "bytes downloaded.")

}

  • 写回答

1条回答 默认 最新

  • dongmangzong8006 2019-03-04 16:24
    关注

    I do not know VBA, but it seems to me what you are doing a HTTP POST with the credentials, and then you are doing a HTTP GET to obtain the file you are looking for. I guess the class you are using keeps the cookies from request to request and that is how the authentication works.

    Assuming that, in Go you would use the library https://golang.org/pkg/net/http/cookiejar/ to keep the cookies and https://golang.org/pkg/net/http/ to do the actual requests.

    You pass each time the same cookie jar.

    Something along these lines (not exact or checked):

    package main
    
    import (
        "net/http"
        "net/http/cookiejar"
    )
    
    func main() {
        cookieJar, _ := cookiejar.New(nil)
    
        client := &http.Client{
            Jar: cookieJar,
        }
    
        resp, err := http.PostForm("http://example.com/loginform",
            url.Values{"login": {"sdiscor"}, "password": {"sdiscor"}})
        resp, err := client.Get("http://example.com/")
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?