Download file and change the charset of page charset.
(p *page.Page, req *request.Request)
| 184 | |
| 185 | // Download file and change the charset of page charset. |
| 186 | func (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 | |
| 229 | func (this *HttpDownloader) downloadHtml(p *page.Page, req *request.Request) *page.Page { |
| 230 | var err error |
no test coverage detected