Allow setting custom user agent string.

This commit is contained in:
A. Svensson 2015-11-30 19:30:01 +01:00
parent cf505b97c7
commit fb6cebc8dc
1 changed files with 16 additions and 1 deletions

View File

@ -34,6 +34,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
@ -51,6 +52,8 @@ type Document struct {
Entity map[string]string // Mapping of custom entity conversions.
Root *Node // The document's root node.
SaveDocType bool // Whether not to include the XML doctype in saves.
useragent string // Used internally
}
// Create a new, empty XML document instance.
@ -194,7 +197,14 @@ func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error)
// client.
func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error) {
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
}
@ -242,3 +252,8 @@ func (this *Document) SaveStream(w io.Writer) (err error) {
_, err = w.Write(this.SaveBytes())
return
}
// Set a custom user agent when making a new request.
func (this *Document) SetUserAgent(s string) {
this.useragent = s
}