Esempio n. 1
0
    private void Send(string data, int redirections)
    {
        _data = data;
        Encoding encode = Encoding.GetEncoding(_charset);

        _headers.Remove("Content-Length");
        if (!string.IsNullOrEmpty(data) && string.Compare(_method, "post", true) == 0)
        {
            _headers["Content-Length"] = string.Concat(Encoding.GetEncoding(_charset).GetBytes(data).Length);
            if (string.IsNullOrEmpty(_headers["Content-Type"]))
            {
                _headers["Content-Type"] = "application/x-www-form-urlencoded; charset=" + _charset;
            }
            else if (_headers["Content-Type"].IndexOf("multipart/form-data") == -1)
            {
                if (_headers["Content-Type"].IndexOf("application/x-www-form-urlencoded") == -1)
                {
                    _headers["Content-Type"] += "; application/x-www-form-urlencoded";
                }
                if (_headers["Content-Type"].IndexOf("charset=") == -1)
                {
                    _headers["Content-Type"] += "; charset=" + _charset;
                }
            }
            data += "\r\n\r\n";
        }
        Uri uri = new Uri(_action);

        if (_cookieContainer != null)
        {
            CookieContainer cc = new CookieContainer();
            if (_headers["Cookie"] != null)
            {
                cc.SetCookies(uri, _headers["Cookie"]);
            }
            Uri uri2 = new Uri(uri.AbsoluteUri.Insert(uri.Scheme.Length + 3, "httprequest."));
            CookieCollection cookies = _cookieContainer.GetCookies(uri);
            foreach (Cookie cookie in cookies)
            {
                cc.SetCookies(uri, string.Concat(cookie));
            }
            cookies = _cookieContainer.GetCookies(uri2);
            foreach (Cookie cookie in cookies)
            {
                cc.SetCookies(uri, string.Concat(cookie));
            }
            _headers["Cookie"] = cc.GetCookieHeader(uri);
            if (string.IsNullOrEmpty(_headers["Cookie"]))
            {
                _headers.Remove("Cookie");
            }
        }
        _headers["Host"] = uri.Authority;
        string http = _method + " " + uri.PathAndQuery + " HTTP/1.1\r\n";

        foreach (string head in _headers)
        {
            http += head + ": " + _headers[head] + "\r\n";
        }
        http += "\r\n" + data;
        _head = http;
        if (_proxy != null)
        {
            HttpProxyRequest pr = new HttpProxyRequest();
            pr.Method     = _method;
            pr.Action     = _action;
            pr.Charset    = _charset;
            pr.Head       = _head;
            pr.Data       = data;
            pr.Connection = _proxyConnection;
            pr.Timeout    = _timeout;
            pr.MaximumAutomaticRedirections = _maximumAutomaticRedirections;
            HttpProxyResponse response = _proxy.Send(pr);
            Action    = response.RequestAction;
            _method   = response.RequestMethod;
            _headers  = HttpRequest.ParseHttpRequestHeader(response.RequestHead);
            _response = new HttpResponse(this, response.ResponseHead);
            _response.SetStream(response.Response);
        }
        else
        {
            byte[] request = encode.GetBytes(http);
            if (_client == null || _remote == null || string.Compare(_remote.Authority, uri.Authority, true) != 0)
            {
                _remote = uri;
                this.Close();
                _client = new TcpClient(uri.Host, uri.Port);
            }
            try {
                _stream = getStream(uri);
                _stream.Write(request, 0, request.Length);
            } catch {
                this.Close();
                _client = new TcpClient(uri.Host, uri.Port);
                _stream = getStream(uri);
                _stream.Write(request, 0, request.Length);
            }
            receive(_stream, redirections, uri, encode);
        }
    }
Esempio n. 2
0
 public abstract HttpProxyResponse Send(HttpProxyRequest request);