拦截 golang 中的进程响应

huangapple go评论88阅读模式
英文:

Intercepting responses to a process in golang

问题

我有一个激活浏览器的过程,它向本地服务器发出请求。
服务器应该做出响应,但我不知道如何在客户端上查看答案。
我希望是浏览器发出请求。我不想使用http.NewRequest自己编写它。

client.go:

func openChrome() {
    
  var page = "https://localhost:1333/"

  program := "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
  url := []string{"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", page}
  attr := &os.ProcAttr{
    Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
  }

  proc,err:=os.StartProcess(program, url, attr)
  proc.Wait();

  // if err!=nil, log.Fatal(err)
}
英文:

I have a process that activates a browser, which makes a request to a local server.
The server should respond but I do not know how to see, client side, the answer.
I need that is the browser that makes the request. I do not want to write it myself with http.NewRequest.

client.go:

func openChrome() {
    
  var page = "https://localhost:1333/"

  program := "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
  url := []string{"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", page}
  attr := &os.ProcAttr{
    Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
  }

  proc,err:=os.StartProcess(program, url, attr)
  proc.Wait();

  // if err!=nil, log.Fatal(err)
}

答案1

得分: 1

为了查看响应并更轻松地控制请求,可以使用像chromedpchromedriver(与webdriver一起)或selenium这样的工具来加载页面并在浏览器中查找响应。所有这些工具都可以在不同程度上从Go语言中访问,并且可以用于驱动浏览器执行标准的请求循环,就像人类一样(加载内容并查询加载的内容)。

您可以查看您启动的进程的stdout和stderr,但这对于这个特定的任务可能没有帮助,所以我忽略了这一点。

您没有给出排除直接请求的理由,但是为了完全控制,这将是另一种可能性,除非您需要渲染HTML,否则您的代码可以执行浏览器可以执行的所有操作(更改用户代理、解析HTML等)。

英文:

To see the response and have easier control over requests could use a tool like chromedp, chromedriver (with webdriver) or selenium to load pages and find the response in the browser. All of these should be accessible to varying degrees from go, and can be used to drive the browser to go through the standard request cycle as if a human were doing it (to load content and query what is loaded).

You can see the stdout and stderr from a process you launch but that is unlikely to help you for this particular task so I'm ignoring that.

You don't give a reason for excluding a direct request, but for full control this would be another possibility, and unless you need to render html your code can do everything a browser could do (change user agent, parse html etc).

huangapple
  • 本文由 发表于 2017年8月24日 15:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/45855881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定