Allow setting custom user agent string.
This commit is contained in:
parent
cf505b97c7
commit
fb6cebc8dc
17
document.go
17
document.go
|
@ -34,6 +34,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -51,6 +52,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 +197,14 @@ 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 {
|
||||||
|
log.Fatalln(err) // TODO
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", this.useragent)
|
||||||
|
|
||||||
|
if r, err = client.Do(req); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,3 +252,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