Merge pull request #24 from lmas/master
Allow setting custom user agent
This commit is contained in:
commit
76f54ee732
18
document.go
18
document.go
|
@ -51,6 +51,8 @@ type Document struct {
|
||||||
Entity map[string]string // Mapping of custom entity conversions.
|
Entity map[string]string // Mapping of custom entity conversions.
|
||||||
Root *Node // The document's root node.
|
Root *Node // The document's root node.
|
||||||
SaveDocType bool // Whether not to include the XML doctype in saves.
|
SaveDocType bool // Whether not to include the XML doctype in saves.
|
||||||
|
|
||||||
|
useragent string // Used internally
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new, empty XML document instance.
|
// Create a new, empty XML document instance.
|
||||||
|
@ -194,7 +196,16 @@ func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error)
|
||||||
// client.
|
// client.
|
||||||
func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error) {
|
func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error) {
|
||||||
var r *http.Response
|
var r *http.Response
|
||||||
if r, err = client.Get(uri); err != nil {
|
|
||||||
|
req, err := http.NewRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(this.useragent) > 1 {
|
||||||
|
req.Header.Set("User-Agent", this.useragent)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = client.Do(req); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,3 +253,8 @@ func (this *Document) SaveStream(w io.Writer) (err error) {
|
||||||
_, err = w.Write(this.SaveBytes())
|
_, err = w.Write(this.SaveBytes())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set a custom user agent when making a new request.
|
||||||
|
func (this *Document) SetUserAgent(s string) {
|
||||||
|
this.useragent = s
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue