当前位置: 首页>>代码示例>>Golang>>正文


Golang http.Response类代码示例

本文整理汇总了Golang中net/http.Response的典型用法代码示例。如果您正苦于以下问题:Golang Response类的具体用法?Golang Response怎么用?Golang Response使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ResponseToJso

func ResponseToJso(o *otto.Object, w *http.Response) {
	o.Set("status", w.Status)
	o.Set("header", w.Header)
	o.Set("cookies", w.Cookies())
	o.Set("statusCode", w.StatusCode)
	o.Set("proto", w.Proto)
	c, _ := ioutil.ReadAll(w.Body)
	o.Set("body", string(c))
	if w.TLS != nil {
		o.Set("tlsServerName", w.TLS.ServerName)
		o.Set("tlsNegotiatedProtocol", w.TLS.NegotiatedProtocol)
		names := []string{}
		ips := []string{}
		for _, k := range w.TLS.PeerCertificates {
			for _, n := range k.DNSNames {
				names = append(names, n)
			}
			for _, n := range k.IPAddresses {
				ips = append(ips, n.String())
			}
		}
		o.Set("tlsDNSNames", names)
		o.Set("tlsIPs", ips)
	}
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:25,代码来源:http.go

示例2: TestOAuth2Provider_Non200Response

func TestOAuth2Provider_Non200Response(t *testing.T) {

	config := &common.Config{
		Map: objx.MSI(
			OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
			OAuth2KeyScope, OAuth2KeyScope,
			OAuth2KeyClientID, OAuth2KeyClientID,
			OAuth2KeySecret, OAuth2KeySecret,
			OAuth2KeyAuthURL, OAuth2KeyAuthURL,
			OAuth2KeyTokenURL, OAuth2KeyTokenURL)}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testProvider := new(test.TestProvider)

	testResponse := new(http.Response)
	testResponse.Header = make(http.Header)
	testResponse.Header.Set("Content-Type", "text/plain")
	testResponse.StatusCode = 401
	testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))

	testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	data := objx.MSI(OAuth2KeyCode, []string{"123"})
	_, err := CompleteAuth(testTripperFactory, data, config, testProvider)

	if assert.Error(t, err) {
		assert.IsType(t, &common.AuthServerError{}, err)
	}

	mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)

}
开发者ID:jmptrader,项目名称:gomniauth,代码行数:34,代码来源:complete_auth_test.go

示例3: handleResp

// handleResp handles the responses from the etcd server
// If status code is OK, read the http body and return it as byte array
// If status code is TemporaryRedirect, update leader.
// If status code is InternalServerError, sleep for 200ms.
func (c *Client) handleResp(resp *http.Response) (bool, []byte) {
	defer resp.Body.Close()

	code := resp.StatusCode

	if code == http.StatusTemporaryRedirect {
		u, err := resp.Location()

		if err != nil {
			logger.Warning(err)
		} else {
			c.cluster.updateLeaderFromURL(u)
		}

		return false, nil

	} else if code == http.StatusInternalServerError {
		time.Sleep(time.Millisecond * 200)

	} else if validHttpStatusCode[code] {
		b, err := ioutil.ReadAll(resp.Body)

		if err != nil {
			return false, nil
		}

		return true, b
	}

	logger.Warning("bad status code ", resp.StatusCode)
	return false, nil
}
开发者ID:kula,项目名称:etcdctl,代码行数:36,代码来源:requests.go

示例4: getSessionAndAppPort

func getSessionAndAppPort(url string, rPort uint16) (*http.Cookie, *http.Cookie, string) {
	var client http.Client
	var req *http.Request
	var resp *http.Response
	var err error
	var port []byte

	uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)

	req, err = http.NewRequest("GET", uri, nil)
	Expect(err).ToNot(HaveOccurred())

	resp, err = client.Do(req)
	Expect(err).ToNot(HaveOccurred())

	port, err = ioutil.ReadAll(resp.Body)
	Expect(err).ToNot(HaveOccurred())

	var sessionCookie, vcapCookie *http.Cookie
	for _, cookie := range resp.Cookies() {
		if cookie.Name == proxy.StickyCookieKey {
			sessionCookie = cookie
		} else if cookie.Name == proxy.VcapCookieId {
			vcapCookie = cookie
		}
	}

	return sessionCookie, vcapCookie, string(port)
}
开发者ID:rakutentech,项目名称:gorouter,代码行数:29,代码来源:router_test.go

示例5: TestDealWithItParentTest

func TestDealWithItParentTest(t *testing.T) {
	stub := StubHandler().(*stubHandler)
	target := ChartHandler(make(chan bool), &Results{}, stub).(*chartHandler)

	expectedData := []byte("Expected Data")
	var response http.Response
	response.Body = nopCloser{bytes.NewBuffer(expectedData)}

	testTime := time.Now()
	timer := StubTimer(testTime, testTime.Add(time.Hour))

	go func() {
		<-target.logger
	}()
	target.DealWithIt(response, &timer)

	if stub.dealCalled == 0 {
		t.Errorf("TestDealWithItParent: Failed to call DealWithIt on the parent")
	}

	result, err := ioutil.ReadAll(stub.savedBody)
	if err != nil {
		t.Errorf("TestDealWithItParent: Failed to read expected data from parent call, err %v", err)
	}
	if !bytes.Equal(result, expectedData) {
		t.Errorf("TestDealWithItParent: Expected '%v' but got '%v'", expectedData, result)
	}

}
开发者ID:gawth,项目名称:gravo,代码行数:29,代码来源:charthandler_test.go

示例6: prepareResponseHeaders

func prepareResponseHeaders(res *http.Response) {
	// remove global hop-by-hop headers.
	for _, h := range hopHeaders {
		res.Header.Del(h)
	}

	// remove the Upgrade header and headers referenced in the Connection
	// header if HTTP < 1.1 or if Connection header didn't contain "upgrade":
	// https://tools.ietf.org/html/rfc7230#section-6.7
	if !res.ProtoAtLeast(1, 1) || !isConnectionUpgrade(res.Header) {
		res.Header.Del("Upgrade")

		// A proxy or gateway MUST parse a received Connection header field before a
		// message is forwarded and, for each connection-option in this field, remove
		// any header field(s) from the message with the same name as the
		// connection-option, and then remove the Connection header field itself (or
		// replace it with the intermediary's own connection options for the
		// forwarded message): https://tools.ietf.org/html/rfc7230#section-6.1
		tokens := strings.Split(res.Header.Get("Connection"), ",")
		for _, hdr := range tokens {
			res.Header.Del(hdr)
		}
		res.Header.Del("Connection")
	}
}
开发者ID:imjorge,项目名称:flynn,代码行数:25,代码来源:reverseproxy.go

示例7: Do

// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
	if client == nil {
		client = http.DefaultClient
	}

	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
	cancel := canceler(client, req)

	type responseAndError struct {
		resp *http.Response
		err  error
	}
	result := make(chan responseAndError, 1)

	// Make local copies of test hooks closed over by goroutines below.
	// Prevents data races in tests.
	testHookDoReturned := testHookDoReturned
	testHookDidBodyClose := testHookDidBodyClose

	go func() {
		resp, err := client.Do(req)
		testHookDoReturned()
		result <- responseAndError{resp, err}
	}()

	var resp *http.Response

	select {
	case <-ctx.Done():
		testHookContextDoneBeforeHeaders()
		cancel()
		// Clean up after the goroutine calling client.Do:
		go func() {
			if r := <-result; r.resp != nil {
				testHookDidBodyClose()
				r.resp.Body.Close()
			}
		}()
		return nil, ctx.Err()
	case r := <-result:
		var err error
		resp, err = r.resp, r.err
		if err != nil {
			return resp, err
		}
	}

	c := make(chan struct{})
	go func() {
		select {
		case <-ctx.Done():
			cancel()
		case <-c:
			// The response's Body is closed.
		}
	}()
	resp.Body = &notifyingReader{resp.Body, c}

	return resp, nil
}
开发者ID:CowLeo,项目名称:distribution,代码行数:63,代码来源:ctxhttp.go

示例8: transformResponse

func (c *imageTransformer) transformResponse(r *http.Response) {

	var images []APIImages

	if err := json.NewDecoder(r.Body).Decode(&images); err != nil {
		return
	}

	for _, im := range images {

		if im.Labels == nil {
			im.Labels = make(map[string]string)
		}

		im.Labels["hola"] = "world"
	}

	var b bytes.Buffer
	w := bufio.NewWriter(&b)

	// Now take the struct and encode it
	if err := json.NewEncoder(w).Encode(&images); err != nil {
		return
	}

	// Restore the io.ReadCloser to its original state
	r.Body = ioutil.NopCloser(bytes.NewBuffer(b.Bytes()))

	// Set size of modified body
	r.ContentLength = int64(binary.Size(b))
}
开发者ID:morfeo8marc,项目名称:docker-multi-tenancy,代码行数:31,代码来源:image.go

示例9: streamAndCache

// Code to stream the result in lock-step to the client and to a cache file.
// If conn terminates, continue streaming the response into the cache.
func streamAndCache(res *http.Response, conn net.Conn, cache_path string) int64 {
	response_reader, cache_writer := io.Pipe()
	defer func() {
		err := cache_writer.Close()
		if err != nil {
			panic(err)
		}
	}()

	n_recvd := make(chan int64)
	go func() {
		n_recvd <- CacheResponse(cache_path, response_reader)
	}()

	// Swallow write errors to `conn` since we want it to keep writing to the
	// cache even if conn goes away
	w := io.MultiWriter(cache_writer, writeErrorSwallower{conn})
	err := res.Write(w)
	if err != nil {
		panic(err)
	}

	// Wait until CacheResponse is done copying the response to a file.
	n := <-n_recvd
	return n
}
开发者ID:khia,项目名称:httpcache,代码行数:28,代码来源:main.go

示例10: traceHttpResponse

func traceHttpResponse(c *Configuration, res *http.Response) {
	if res == nil {
		return
	}

	tracerx.Printf("HTTP: %d", res.StatusCode)

	if c.isTracingHttp == false {
		return
	}

	fmt.Fprintf(os.Stderr, "\n")
	fmt.Fprintf(os.Stderr, "< %s %s\n", res.Proto, res.Status)
	for key, _ := range res.Header {
		fmt.Fprintf(os.Stderr, "< %s: %s\n", key, res.Header.Get(key))
	}

	traceBody := false
	ctype := strings.ToLower(strings.SplitN(res.Header.Get("Content-Type"), ";", 2)[0])
	for _, tracedType := range tracedTypes {
		if strings.Contains(ctype, tracedType) {
			traceBody = true
		}
	}

	res.Body = newCountedResponse(res)
	if traceBody {
		res.Body = newTracedBody(res.Body)
	}

	fmt.Fprintf(os.Stderr, "\n")
}
开发者ID:miumok98,项目名称:git-lfs,代码行数:32,代码来源:http.go

示例11: handlerWriteResponse

func (srv *Server) handlerWriteResponse(request *Request, res *http.Response, c net.Conn, bw *bufio.Writer) error {
	// Setup write stage
	request.startPipelineStage("server.ResponseWrite")
	request.CurrentStage.Type = PipelineStageTypeOverhead

	// cleanup
	defer func() {
		request.finishPipelineStage()
		request.finishRequest()
		srv.requestFinished(request, res)
		if res.Body != nil {
			res.Body.Close()
		}
	}()

	// Cycle nodelay flag on socket
	// Note: defers for FILO so this will happen before the write
	// 		 phase is complete, which is what we want.
	if nodelay := srv.setNoDelay(c, false); nodelay {
		defer srv.setNoDelay(c, true)
	}

	var err error
	// Write response
	if err = res.Write(bw); err != nil {
		return err
	}

	// Flush any remaining buffer
	err = bw.Flush()

	return err
}
开发者ID:dcmrlee,项目名称:falcore,代码行数:33,代码来源:server.go

示例12: SendForTraversal

func (session *Session) SendForTraversal(url string, data string) (string, error) {
	var (
		resp *http.Response // http response
		buf  bytes.Buffer   // contains http response body
		err  error
	)
	resp, err = session.request(url, data)
	if err != nil {
		return "", err
	}
	log.Println(resp)
	defer func() {
		if resp.Body != nil {
			resp.Body.Close()
		}
	}()
	_, err = buf.ReadFrom(resp.Body)
	if err != nil {
		return "", err
	}
	session.StatusCode = resp.StatusCode
	location, err := resp.Location()
	if err != nil {
		return "", err
	}
	session.Location = location.String()
	return buf.String(), nil
}
开发者ID:cloudcube,项目名称:goneo4j,代码行数:28,代码来源:session.go

示例13: RoundTrip

func (lrt *LoggingRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
	var err error
	var res *http.Response

	log.Printf("Forwarding to: %s\n", request.URL.String())
	res, err = lrt.transport.RoundTrip(request)
	if err != nil {
		return nil, err
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Println("")
	log.Printf("Response Headers: %#v\n", res.Header)
	log.Println("")
	log.Printf("Response Body: %s\n", string(body))
	log.Println("")
	res.Body = ioutil.NopCloser(bytes.NewBuffer(body))

	log.Println("Sending response to GoRouter...")

	return res, err
}
开发者ID:asifhuawei,项目名称:logging-route-service,代码行数:25,代码来源:main.go

示例14: Download

func (self *Surfer) Download(sp *spider.Spider, cReq *request.Request) *spider.Context {
	ctx := spider.GetContext(sp, cReq)

	var resp *http.Response
	var err error

	switch cReq.GetDownloaderID() {
	case SURF_ID:
		resp, err = self.surf.Download(cReq)

	case PHANTOM_ID:
		resp, err = self.phantom.Download(cReq)
	}

	if resp.StatusCode >= 400 {
		err = errors.New("响应状态 " + resp.Status)
	}

	if resp.Header.Get("Content-Encoding") == "gzip" {
		var gzipReader *gzip.Reader
		gzipReader, err = gzip.NewReader(resp.Body)
		resp.Body.Close()
		if err == nil {
			resp.Body = ioutil.NopCloser(gzipReader)
		}
	}

	ctx.SetResponse(resp).SetError(err)

	return ctx
}
开发者ID:ReinhardHsu,项目名称:pholcus,代码行数:31,代码来源:downloader_surfer.go

示例15: downloadFile

//下载文件,并对字符编码做相应的处理
func (this *HttpDownLoader) downloadFile(p *page.Page, req *page.Request) (*page.Page, string) {
	var err error
	var httpResp *http.Response
	var urlStr string
	var method string
	urlStr = req.GetUrl()
	if len(urlStr) == 0 {
		logs.GetFirstLogger().Error("url is empty")
		p.SetStatus(true, "url is empty")
		return p, ""
	}

	method = req.GetMethod()

	if method == "POST" {
		httpResp, err = http.Post(req.GetUrl(), "application/x-www-form-urlencoded", strings.NewReader(req.GetPostData()))
	} else {
		httpResp, err = http.Get(req.GetUrl())
	}

	if err != nil {
		logs.GetFirstLogger().Error("http visit error :" + err.Error())
		p.SetStatus(true, err.Error())
	}
	p.SetHeader(httpResp.Header)
	p.SetCookies(httpResp.Cookies())
	body, _ := ioutil.ReadAll(httpResp.Body)
	bodyStr := string(body)
	defer httpResp.Body.Close()
	return p, bodyStr
}
开发者ID:dulumao,项目名称:game_spider,代码行数:32,代码来源:downloader_http.go


注:本文中的net/http.Response类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。