Esempio n. 1
0
 public QQHttpRequest CreateHttpRequest(string method, string url)
 {
     QQHttpRequest req = new QQHttpRequest(url, method);
     req.AddHeader("User-Agent", _userAgent ?? QQConstants.USER_AGENT);
     req.AddHeader("Referer", QQConstants.REFFER);
     return req;
 }
Esempio n. 2
0
        public Task<QQHttpResponse> ExecuteHttpRequest(QQHttpRequest request, IQQHttpListener listener)
        {
            return Task.Run(() =>
            {
                try
                {
                    var httpItem = new HttpItem()
                    {
                        KeepAlive = true,
                        ProtocolVersion = HttpVersion.Version10,
                        ContentType = "application/x-www-form-urlencoded; charset=UTF-8", // post方法的时候必须填写,要不然服务器无法解析
                        Encoding = Encoding.UTF8,
                        Allowautoredirect = true,
                        Method = request.Method,
                        URL = request.Url,
                        ReadWriteTimeout = (request.ReadTimeout > 0) ? request.ReadTimeout : 100000,
                        Timeout = (request.ConnectTimeout > 0) ? request.ConnectTimeout : 30000,
                        ResultType = ResultType.Byte,
                    };

                    if (request.HeaderMap.ContainsKey("User-Agent"))
                    {
                        httpItem.UserAgent = request.HeaderMap["User-Agent"];
                        request.HeaderMap.Remove("User-Agent");
                    }
                    if (request.HeaderMap.ContainsKey("Referer"))
                    {
                        httpItem.Referer = request.HeaderMap["Referer"];
                        request.HeaderMap.Remove("Referer");
                    }

                    foreach (var header in request.HeaderMap)
                    {
                        httpItem.Header.Add(header.Key, header.Value);
                    }
                    httpItem.CookieContainer = _cookieContainer;

                    if (request.Method.Equals("POST"))
                    {
                        if (request.FileMap.Count > 0)
                        {
                            httpItem.PostDataType = PostDataType.FilePath;
                            httpItem.Postdata = "";
                        }
                        else if (request.PostMap.Count > 0)
                        {
                            httpItem.PostDataType = PostDataType.String;
                            httpItem.Postdata = request.InputString;
                        }
                    }
                    else if (request.Method.Equals("GET"))
                    {

                    }
                    else
                    {
                        throw new QQException(QQErrorCode.IO_ERROR, "not support http method:" + request.Method);
                    }

                    HttpResult result = new HttpHelper().GetHtml(httpItem);
                    QQHttpResponse response = new QQHttpResponse()
                    {
                        ResponseMessage = result.StatusDescription,
                        ResponseCode = (int)result.StatusCode,
                        ResponseData = result.ResultByte,
                        Headers = new Dictionary<string, List<string>>(),
                    };
                    if (result.Header != null)
                    {
                        foreach (string header in result.Header)
                        {
                            response.Headers.Add(header, result.Header[header]);
                        }
                    }
                    if (result.CookieCollection != null)
                    {
                        _cookieContainer.Add(result.CookieCollection);
                    }

                    if (listener != null)
                    {
                        listener.OnHttpHeader(response);
                        listener.OnHttpRead(0, response.GetContentLength());
                        listener.OnHttpFinish(response);
                    }
                    return response;
                }
                catch (IOException e)
                {
                    throw new QQException(QQErrorCode.IO_ERROR, e);
                }
                catch (Exception e)
                {
                    throw new QQException(QQErrorCode.UNKNOWN_ERROR, e);
                }
            });
        }