Added HTTP Request With Cookie

master
ice3man 2018-04-01 18:11:57 +05:30
parent 13e0c9e013
commit 0b004f916d
1 changed files with 50 additions and 0 deletions

View File

@ -10,6 +10,8 @@ package helper
import (
"net/http"
"net/url"
"net/http/cookiejar"
"crypto/tls"
"time"
)
@ -51,3 +53,51 @@ func GetHTTPResponse(url string, timeout int) (resp *http.Response, err error) {
return resp, nil
}
//
// GetHTTPCookieResponse : Returns a HTTP Response object with a cookie object containing current cookies
// @param url : URL To Visit (Note, It needs full url with scheme)
// @params cookies : Cookies to send with the request
// @param timeout : Seconds to wait for response until timeout
//
// @return resp : HTTP Response object
// @return cookie : Cookies recieved with the Request
// @return err : nil if successfull else error
//
func GetHTTPCookieResponse(urls string, cookies []*http.Cookie, timeout int) (resp *http.Response, cookie []*http.Cookie, err error) {
var curCookieJar *cookiejar.Jar
curCookieJar, _ = cookiejar.New(nil)
// Add the cookies recieved via request params
u, _ := url.Parse(urls)
curCookieJar.SetCookies(u, cookies)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Jar: curCookieJar,
Timeout: time.Duration(timeout) * time.Second,
}
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return resp, cookie, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1")
req.Header.Add("Connection", "close")
resp, err = client.Do(req)
if err != nil {
return resp, cookie, err
}
cookie = curCookieJar.Cookies(req.URL)
return resp, cookie, nil
}