本文整理汇总了Golang中net/http.RoundTripper.RoundTrip方法的典型用法代码示例。如果您正苦于以下问题:Golang RoundTripper.RoundTrip方法的具体用法?Golang RoundTripper.RoundTrip怎么用?Golang RoundTripper.RoundTrip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.RoundTripper
的用法示例。
在下文中一共展示了RoundTripper.RoundTrip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: gatherURL
func (p *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
collectDate := time.Now()
var req, err = http.NewRequest("GET", url, nil)
req.Header.Add("Accept", acceptHeader)
var token []byte
var resp *http.Response
tlsCfg, err := internal.GetTLSConfig(
p.SSLCert, p.SSLKey, p.SSLCA, p.InsecureSkipVerify)
if err != nil {
return err
}
var rt http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: tlsCfg,
ResponseHeaderTimeout: time.Duration(3 * time.Second),
DisableKeepAlives: true,
}
if p.BearerToken != "" {
token, err = ioutil.ReadFile(p.BearerToken)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+string(token))
}
resp, err = rt.RoundTrip(req)
if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading body: %s", err)
}
metrics, err := Parse(body, resp.Header)
if err != nil {
return fmt.Errorf("error reading metrics for %s: %s",
url, err)
}
// Add (or not) collected metrics
for _, metric := range metrics {
tags := metric.Tags()
tags["url"] = url
acc.AddFields(metric.Name(), metric.Fields(), tags, collectDate)
}
return nil
}
示例2: main
func main() {
//Set URL to test --- test will NOT follow redirects
testurl := "https://beacon.walmart.com/vm/ttap.gif?id=10694084&160x600&audience=&creative=1027467&creativetype=rich_media&device=&initiative=209902&placement=SSLTEST&targeting=wmxaudience&vendor=AOD&version=2015802"
// Set log filename
logfile := "testlog.txt"
var DefaultTransport http.RoundTripper = &http.Transport{}
// Set number of requests
for i := 0; i < 500000; i++ {
req, _ := http.NewRequest("GET", testurl, nil)
resp, _ := DefaultTransport.RoundTrip(req)
logger, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer logger.Close()
// Grab specified headers from HTTP response
location := resp.Header.Get("Location")
cookie := resp.Header.Get("Set-Cookie")
t := time.Now().UTC().String()
result := fmt.Sprintf("%s\t%s\t%s\r\n", t, location, cookie)
logger.WriteString(result)
// Wait time before next request
time.Sleep(time.Second)
}
}
示例3: start
// Dial dials to the remote url, and sends streaming request. If it succeeds,
// it returns nil error, and the caller should call Handle function to keep
// receiving appendEntry messages.
func (s *streamClient) start(tr http.RoundTripper, u string, cid types.ID) error {
uu, err := url.Parse(u)
if err != nil {
return fmt.Errorf("parse url %s error: %v", u, err)
}
uu.Path = path.Join(RaftStreamPrefix, s.id.String())
req, err := http.NewRequest("GET", uu.String(), nil)
if err != nil {
return fmt.Errorf("new request to %s error: %v", u, err)
}
req.Header.Set("X-Etcd-Cluster-ID", cid.String())
req.Header.Set("X-Raft-To", s.to.String())
req.Header.Set("X-Raft-Term", strconv.FormatUint(s.term, 10))
resp, err := tr.RoundTrip(req)
if err != nil {
return fmt.Errorf("error posting to %q: %v", u, err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return fmt.Errorf("unhandled http status %d", resp.StatusCode)
}
s.closer = resp.Body
go s.handle(resp.Body)
log.Printf("rafthttp: starting client stream to %s at term %d", s.to, s.term)
return nil
}
示例4: RoundTrip
// RoundTrip implements the RoundTripper interface.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
var transport http.RoundTripper
if t.Transport != nil {
transport = t.Transport
} else {
transport = http.DefaultTransport
}
// To set extra querystring params, we must make a copy of the Request so
// that we don't modify the Request we were given. This is required by the
// specification of http.RoundTripper.
req = cloneRequest(req)
child := t.Recorder.Child()
if t.SetName {
child.Name(req.URL.Host)
}
SetSpanIDHeader(req.Header, child.SpanID)
e := NewClientEvent(req)
e.ClientSend = time.Now()
// Make the HTTP request.
resp, err := transport.RoundTrip(req)
e.ClientRecv = time.Now()
if err == nil {
e.Response = responseInfo(resp)
} else {
e.Response.StatusCode = -1
}
child.Event(e)
return resp, err
}
示例5: RoundTrip
func (t *tracingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var u http.RoundTripper
if t.Transport != nil {
u = t.Transport
} else {
u = http.DefaultTransport
}
reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
t.Writer.Write(reqBytes)
resp, err := u.RoundTrip(req)
if err != nil {
return nil, err
}
respBytes, err := httputil.DumpResponse(resp, true)
if err != nil {
return nil, err
}
t.Writer.Write(respBytes)
return resp, nil
}
示例6: newStreamReader
// newStreamClient starts and returns a new started stream client.
// The caller should call stop when finished, to shut it down.
func newStreamReader(id, to, cid types.ID, term uint64, tr http.RoundTripper, u string, r Raft) (*streamReader, error) {
s := &streamReader{
id: id,
to: to,
term: term,
r: r,
done: make(chan struct{}),
}
uu, err := url.Parse(u)
if err != nil {
return nil, fmt.Errorf("parse url %s error: %v", u, err)
}
uu.Path = path.Join(RaftStreamPrefix, s.id.String())
req, err := http.NewRequest("GET", uu.String(), nil)
if err != nil {
return nil, fmt.Errorf("new request to %s error: %v", u, err)
}
req.Header.Set("X-Etcd-Cluster-ID", cid.String())
req.Header.Set("X-Raft-To", s.to.String())
req.Header.Set("X-Raft-Term", strconv.FormatUint(s.term, 10))
resp, err := tr.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("error posting to %q: %v", u, err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("unhandled http status %d", resp.StatusCode)
}
s.closer = resp.Body
go s.handle(resp.Body)
log.Printf("rafthttp: starting client stream to %s at term %d", s.to, s.term)
return s, nil
}
示例7: roundTrip
func roundTrip(rt http.RoundTripper, req *http.Request) (body []byte, err error) {
resp, err := rt.RoundTrip(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
示例8: ByteThrottledRoundTripper
// ByteThrottledRoundTripper wraps another RoundTripper rt,
// throttling all requests to the specified byte rate.
func ByteThrottledRoundTripper(rt http.RoundTripper, rate int64) http.RoundTripper {
freq := time.Duration(1 * time.Millisecond)
bucket := tb.NewBucket(rate, freq)
return roundTripperFunc(func(r *http.Request) (*http.Response, error) {
got := bucket.Take(r.ContentLength)
for got < r.ContentLength {
got += bucket.Take(r.ContentLength - got)
time.Sleep(freq)
}
return rt.RoundTrip(r)
})
}
示例9: ReqThrottledRoundTripper
// ReqThrottledRoundTripper wraps another RoundTripper rt,
// throttling all requests to the specified request rate.
func ReqThrottledRoundTripper(rt http.RoundTripper, rate int64) http.RoundTripper {
freq := time.Duration(1e9 / rate)
bucket := tb.NewBucket(rate, freq)
return roundTripperFunc(func(r *http.Request) (*http.Response, error) {
got := bucket.Take(1)
for got != 1 {
got = bucket.Take(1)
time.Sleep(freq)
}
return rt.RoundTrip(r)
})
}
示例10: request
func request(rt http.RoundTripper, requestURL string, requestHeaders http.Header) (*http.Response, error) {
// Build the request
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}
for k, v := range requestHeaders {
req.Header[k] = v
}
req.Header.Set(CSRFTokenHeader, "1")
// Make the request
return rt.RoundTrip(req)
}
示例11: HTTPGet
// HTTPGet creates a new http request
func HTTPGet(url string, follow, insecure bool, h map[string]string, timeout ...int) (*http.Response, error) {
// timeout in seconds defaults to 5
var t int = 5
if len(timeout) > 0 {
t = timeout[0]
}
// if insecure = true, skip ssl verification
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
ResponseHeaderTimeout: time.Duration(t) * time.Second,
}
client := &http.Client{}
client.Transport = tr
// create a new request
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "epazote")
// set custom headers on request
if h != nil {
for k, v := range h {
req.Header.Set(k, v)
}
}
if follow {
res, err := client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
// not follow redirects
var DefaultTransport http.RoundTripper = tr
res, err := DefaultTransport.RoundTrip(req)
if err != nil {
return nil, err
}
return res, nil
}
示例12: TestTransport
func (S) TestTransport(c *gocheck.C) {
var t http.RoundTripper = &Transport{
Message: "Ok",
Status: http.StatusOK,
Headers: map[string][]string{"Authorization": {"something"}},
}
req, _ := http.NewRequest("GET", "/", nil)
r, err := t.RoundTrip(req)
c.Assert(err, gocheck.IsNil)
c.Assert(r.StatusCode, gocheck.Equals, http.StatusOK)
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
c.Assert(string(b), gocheck.Equals, "Ok")
c.Assert(r.Header.Get("Authorization"), gocheck.Equals, "something")
}
示例13: NewRoundTripper
// NewRoundTripper creates an http.RoundTripper to instrument external requests.
// This RoundTripper must be used in same the goroutine as the other uses of the
// Transaction's SegmentTracer methods. http.DefaultTransport is used if an
// http.RoundTripper is not provided.
//
// client := &http.Client{}
// client.Transport = newrelic.NewRoundTripper(txn, nil)
// resp, err := client.Get("http://example.com/")
//
func NewRoundTripper(txn Transaction, original http.RoundTripper) http.RoundTripper {
return roundTripperFunc(func(request *http.Request) (*http.Response, error) {
segment := StartExternalSegment(txn, request)
if nil == original {
original = http.DefaultTransport
}
response, err := original.RoundTrip(request)
segment.Response = response
segment.End()
return response, err
})
}
示例14: NewHTTPPoolTransport
// NewHTTPPoolTransport returns a function that must be used in groupcache.HTTPPool.Transport.
//
// rt is optional, http.DefaultTransport is used by default.
func NewHTTPPoolTransport(rt http.RoundTripper) func(groupcache.Context) http.RoundTripper {
if rt == nil {
rt = http.DefaultTransport
}
return func(ctx groupcache.Context) http.RoundTripper {
return roundTripperFunc(func(req *http.Request) (*http.Response, error) {
if ctx, ok := ctx.(*Context); ok && ctx != nil {
err := setContext(req, ctx)
if err != nil {
return nil, err
}
}
return rt.RoundTrip(req)
})
}
}
示例15: RoundTrip
// RoundTrip implements the http.RoundTripper interface.
//
// For higher-level HTTP client support (such as handling of cookies
// and redirects), see Get, Post, and the Client type.
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
if req.URL == nil {
closeBody(req)
return nil, errors.New("http: nil Request.URL")
}
if req.Header == nil {
closeBody(req)
return nil, errors.New("http: nil Request.Header")
}
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
t.altMu.RLock()
var rt http.RoundTripper
if t.altProto != nil {
rt = t.altProto[req.URL.Scheme]
}
t.altMu.RUnlock()
if rt == nil {
closeBody(req)
return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
}
return rt.RoundTrip(req)
}
if req.URL.Host == "" {
closeBody(req)
return nil, errors.New("http: no Host in request URL")
}
treq := &transportRequest{Request: req}
cm, err := t.connectMethodForRequest(treq)
if err != nil {
closeBody(req)
return nil, err
}
// Get the cached or newly-created connection to either the
// host (for http or https), the http proxy, or the http proxy
// pre-CONNECTed to https server. In any case, we'll be ready
// to send it requests.
pconn, err := t.getConn(req, cm)
if err != nil {
t.setReqCanceler(req, nil)
closeBody(req)
return nil, err
}
return pconn.roundTrip(treq)
}