Asynchronous 多响应golang的异步请求
我需要向在不同时间返回不同响应的服务器发出请求。我的意思是,服务器生成不同的响应,这些响应需要不同的执行时间,所以服务器会在响应可用时立即返回响应 我想在屏幕上打印出来,一旦服务器返回我,我就会处理这些响应 到目前为止,我所能做的就是打印响应,但仅当服务器返回所有响应时。因此,如果第一个响应需要1秒,最后一个响应需要10秒,那么我的代码需要等待10秒才能打印所有消息 编辑:要添加我拥有的代码:Asynchronous 多响应golang的异步请求,asynchronous,go,request,Asynchronous,Go,Request,我需要向在不同时间返回不同响应的服务器发出请求。我的意思是,服务器生成不同的响应,这些响应需要不同的执行时间,所以服务器会在响应可用时立即返回响应 我想在屏幕上打印出来,一旦服务器返回我,我就会处理这些响应 到目前为止,我所能做的就是打印响应,但仅当服务器返回所有响应时。因此,如果第一个响应需要1秒,最后一个响应需要10秒,那么我的代码需要等待10秒才能打印所有消息 编辑:要添加我拥有的代码: //Config is gotten from yml file RestConfig =
//Config is gotten from yml file
RestConfig = Config["rest"].(map[string]interface{})
ServerConfig = Config["server"].(map[string]interface{})
RequestUrl := ServerConfig["url"]
RequestReader := bytes.NewReader(body)
Request, _ := http.NewRequest("POST", RequestUrl.(string), RequestReader)
//AppendHeaders append the needing headers to the request
client.AppendHeaders(Request, RestConfig["headers"])
//the type of client.HttpClient is *http.Client
Response, _ := client.HttpClient.Do(Request)
//And to print in the screen
defer Response.Body.Close()
fmt.Println( "-> Receiving response:\n---\n" )
fmt.Println( Response , "\n---\n-> Response body:\n---\n")
body_resp, _ := ioutil.ReadAll(Response.Body)
fmt.Println( string(body_resp) )
fmt.Println( "\n--\n")
有什么办法吗
非常感谢。这个包裹正是您要找的
上下文包负责进程和服务器请求的信号取消和操作截止日期。这有两个公共方法:WithCancel和WithTimeout。当请求处理程序返回时,通常会取消与传入请求关联的上下文
对于您的特定情况,您可以使用WithTimeout方法设置对后端服务器的请求的截止日期
// WithTimeout returns a copy of parent whose Done channel is closed as soon as
// parent.Done is closed, cancel is called, or timeout elapses. The new
// Context's Deadline is the sooner of now+timeout and the parent's deadline, if
// any. If the timer is still running, the cancel function releases its
// resources.
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
下面是一段摘自
要进一步阅读,请参阅本文:
这个软件包就是您要找的
上下文包负责进程和服务器请求的信号取消和操作截止日期。这有两个公共方法:WithCancel和WithTimeout。当请求处理程序返回时,通常会取消与传入请求关联的上下文
对于您的特定情况,您可以使用WithTimeout方法设置对后端服务器的请求的截止日期
// WithTimeout returns a copy of parent whose Done channel is closed as soon as
// parent.Done is closed, cancel is called, or timeout elapses. The new
// Context's Deadline is the sooner of now+timeout and the parent's deadline, if
// any. If the timer is still running, the cancel function releases its
// resources.
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
下面是一段摘自
要进一步阅读,请参阅本文:
最后,我的代码如下:
package main
import (
"fmt"
"log"
"bytes"
"strings"
"bufio"
"net/http"
)
func main() {
var body = "The body"
RequestReader := bytes.NewReader([]byte(body))
req, err := http.NewRequest("POST", "the_url", RequestReader)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/xml")
req.Header.Add("Content-Type", "application/xml")
req.Header.Add("AG-Authorization", "key")
req.Header.Add("AG-Forwarded-Hosts", "*")
resp, err := (&http.Client{}).Do(req)
if err != nil {
log.Fatal(err)
}
reader := bufio.NewReader(resp.Body)
message := ""
for {
line, err := reader.ReadBytes('\n')
if err != nil {
log.Fatal(err)
}
message = message + string(line)
if strings.Contains(message, "<!-- End mark for each message -->"){
fmt.Println(message)
message = ""
}
}
}
谢谢大家。最后,我的代码如下:
package main
import (
"fmt"
"log"
"bytes"
"strings"
"bufio"
"net/http"
)
func main() {
var body = "The body"
RequestReader := bytes.NewReader([]byte(body))
req, err := http.NewRequest("POST", "the_url", RequestReader)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/xml")
req.Header.Add("Content-Type", "application/xml")
req.Header.Add("AG-Authorization", "key")
req.Header.Add("AG-Forwarded-Hosts", "*")
resp, err := (&http.Client{}).Do(req)
if err != nil {
log.Fatal(err)
}
reader := bufio.NewReader(resp.Body)
message := ""
for {
line, err := reader.ReadBytes('\n')
if err != nil {
log.Fatal(err)
}
message = message + string(line)
if strings.Contains(message, "<!-- End mark for each message -->"){
fmt.Println(message)
message = ""
}
}
}
谢谢大家。Hi@SimoEndre我不知道该怎么做。我一直在测试示例server.go和1它对我不起作用总是得到0个结果。最重要的是,我下载了google软件包代码,并按照我需要的方式修改了这是我的新代码,它的工作原理与以前完全相同。它发出请求,服务器发送两个响应,第一个响应时间为1秒,第二个响应时间为6秒,go http客户端等待最后一个响应打印它们。你能看看我的代码吗??谢谢。恐怕api服务器在不同的时间间隔内为您的请求提供服务。在这种情况下,你无能为力。还是我遗漏了什么?谢谢@SimoEndre我要和服务器开发团队谈谈。再次您好@SimoEndre我解决了这个问题。问题是在我和最终服务器之间有一个代理,这个代理等待来自最终服务器的所有响应。非常感谢。没问题,谢谢你的反馈:很高兴你解决了这个问题。嗨@SimoEndre,我不知道怎么做。我一直在测试示例server.go和1它对我不起作用总是得到0个结果。最重要的是,我下载了google软件包代码,并按照我需要的方式修改了这是我的新代码,它的工作原理与以前完全相同。它发出请求,服务器发送两个响应,第一个响应时间为1秒,第二个响应时间为6秒,go http客户端等待最后一个响应打印它们。你能看看我的代码吗??谢谢。恐怕api服务器在不同的时间间隔内为您的请求提供服务。在这种情况下,你无能为力。还是我遗漏了什么?谢谢@SimoEndre我要和服务器开发团队谈谈。再次您好@SimoEndre我解决了这个问题。问题是在我和最终服务器之间有一个代理,这个代理等待来自最终服务器的所有响应。非常感谢。没问题,谢谢你的反馈:很高兴你解决了这个问题。