MCPcopy
hub / github.com/hu17889/go_spider / downloadFile

Method downloadFile

core/downloader/downloader_http.go:186–227  ·  view source on GitHub ↗

Download file and change the charset of page charset.

(p *page.Page, req *request.Request)

Source from the content-addressed store, hash-verified

184
185// Download file and change the charset of page charset.
186func (this *HttpDownloader) downloadFile(p *page.Page, req *request.Request) (*page.Page, string) {
187 var err error
188 var urlstr string
189 if urlstr = req.GetUrl(); len(urlstr) == 0 {
190 mlog.LogInst().LogError("url is empty")
191 p.SetStatus(true, "url is empty")
192 return p, ""
193 }
194
195 client := &http.Client{
196 CheckRedirect: req.GetRedirectFunc(),
197 }
198 httpreq, err := http.NewRequest(req.GetMethod(), req.GetUrl(), strings.NewReader(req.GetPostdata()))
199 if header := req.GetHeader(); header != nil {
200 httpreq.Header = req.GetHeader()
201 }
202 if cookies := req.GetCookies(); cookies != nil {
203 for i := range cookies {
204 httpreq.AddCookie(cookies[i])
205 }
206 }
207
208 var resp *http.Response
209 if resp, err = client.Do(httpreq); err != nil {
210 if e, ok := err.(*url.Error); ok && e.Err != nil && e.Err.Error() == "normal" {
211 // normal
212 } else {
213 mlog.LogInst().LogError(err.Error())
214 p.SetStatus(true, err.Error())
215 return p, ""
216 }
217 }
218
219 p.SetHeader(resp.Header)
220 p.SetCookies(resp.Cookies())
221
222 // get converter to utf-8
223 bodyStr := this.changeCharsetEncodingAuto(resp.Header.Get("Content-Type"), resp.Body)
224
225 defer resp.Body.Close()
226 return p, bodyStr
227}
228
229func (this *HttpDownloader) downloadHtml(p *page.Page, req *request.Request) *page.Page {
230 var err error

Callers 3

downloadHtmlMethod · 0.95
downloadJsonMethod · 0.95
downloadTextMethod · 0.95

Calls 13

GetUrlMethod · 0.80
LogErrorMethod · 0.80
SetStatusMethod · 0.80
GetRedirectFuncMethod · 0.80
GetMethodMethod · 0.80
GetPostdataMethod · 0.80
SetHeaderMethod · 0.80
SetCookiesMethod · 0.80
GetMethod · 0.80
CloseMethod · 0.80
GetHeaderMethod · 0.45

Tested by

no test coverage detected