Quantcast
Channel: MarsHut
Viewing all articles
Browse latest Browse all 6551

how to close net/http connection.

$
0
0
hi
I want to use net/http get some file from website. unfortunately, i
found i can't close the connection as i wish, the connection is leak. I
know golang can reclaim the resource. but what i want is to close the
connection immediately. if i remove the dialTimeout function, the
connection will been closed quicky as i wish, so what's wrong with my code?
Thanks

ppmsn

code

package getfile

import (
"fmt"
"net"
"net/http"
"time"

var timeout = time.Duration(3) * time.Second

func dialTimeout(network, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, timeout)
return conn, err

func Getfile() {

transport := http.Transport{
Dial: dialTimeout,

client := 蛛.Client{Transport: 뚶 騫}

//client := 蛛.Client{}
reqest, _ := http.NewRequest("GET", "http://www.baidu.com/img/bdlogo.gif",
nil)

reqest.Header.Set("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
reqest.Header.Set("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3")
reqest.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
reqest.Header.Set("Cache-Control", "max-age=0")
reqest.Header.Set("Connection", "keep-alive")

response, _ := client.Do(reqest)
if response.StatusCode == 200 {
d := make([]byte, 10024)
for {
body, err := response.Body.Read(d)
fmt.Println(body, err)
if (body == 0) || (err != nil) {
break

fmt.Println()

fmt.Println("response.Body.Close()")
response.Body.Close()
response = nil
client = nil

//
main () {

for ;; {
Getfile()

Viewing all articles
Browse latest Browse all 6551

Latest Images

Trending Articles



Latest Images