コード例 #1
0
ファイル: HttpClient.cs プロジェクト: sorisum/uplibrary
        /// <summary>
        /// Send a HTTP request
        /// </summary>
        /// <param name="httpReq">HTTP request</param>
        /// <returns>HTTP response</returns>
        public HttpResponse Send(HttpRequest httpReq)
        {
            // check on parameter
            if (httpReq == null)
            {
                throw new ArgumentNullException("HttpRequest cannot be null !");
            }

            // register write callaback for body request
            httpReq.Body.Write = this.WriteBody;

            // if Host is IP address
            if (httpReq.Uri.HostNameType == UriHostNameType.IPv4)
            {
                this.hostIpEndPoint = new IPEndPoint(IPAddress.Parse(httpReq.Uri.Host), httpReq.Uri.Port);
            }
            // Host is DNS address
            else
            {
                // resolve Host name by DNS
                IPHostEntry hostEntry = Dns.GetHostEntry(httpReq.Uri.Host);
                // check for the first address not null
                // it seems that with .Net Micro Framework, the IPV6 addresses aren't supported and return "null"
                int i = 0;
                while (hostEntry.AddressList[i] == null)
                {
                    i++;
                }
                this.hostIpEndPoint = new IPEndPoint(hostEntry.AddressList[i], httpReq.Uri.Port);
            }

            // add "Host" header
            httpReq.Host = httpReq.Uri.Host + ":" + httpReq.Uri.Port;
            // add "User-Agent" header
            httpReq.UserAgent = HTTP_CLIENT_NAME;

            // create socket and connect
            this.socket = new Socket(this.hostIpEndPoint.Address.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
            this.socket.Connect(this.hostIpEndPoint);

            // send HTTP request (request line and headers)
            this.buffer = Encoding.UTF8.GetBytes(httpReq.ToString());
            this.socket.Send(this.buffer);

            // raise event for allow client to send body request
            this.OnSendBody(httpReq);

            int received = 0;

            this.buffer = new byte[BUFFER_SIZE];
            HttpResponseParserResult result = HttpResponseParserResult.NotCompleted;
            HttpResponseParser       parser = new HttpResponseParser();

            parser.ReceivingBody += parser_ReceivingBody;

            // receive on socket until parse is complete or no data received
            while ((result != HttpResponseParserResult.Completed) &&
                   this.socket.Poll(POLL_TIMEOUT, SelectMode.SelectRead))
            {
                // no data on the socket (closed or timeout)
                if (this.socket.Available == 0)
                {
                    break;
                }

                received = this.socket.Receive(this.buffer, BUFFER_SIZE, SocketFlags.None);
                result   = parser.Parse(this.buffer, received);

                // if the HTTP response is malformed, break
                if (result == HttpResponseParserResult.Malformed)
                {
                    break;
                }
            }

            // unregister write callaback for body request
            httpReq.Body.Write = null;

            // close connection
            this.socket.Close();

            return(parser.Response);
        }
コード例 #2
0
ファイル: HttpClient.cs プロジェクト: ppatierno/uplibrary
        /// <summary>
        /// Send a HTTP request
        /// </summary>
        /// <param name="httpReq">HTTP request</param>
        /// <returns>HTTP response</returns>
        public HttpResponse Send(HttpRequest httpReq)
        {
            // check on parameter
            if (httpReq == null)
                throw new ArgumentNullException("HttpRequest cannot be null !");

            // register write callaback for body request
            httpReq.Body.Write = this.WriteBody;

            // if Host is IP address
            if (httpReq.Uri.HostNameType == UriHostNameType.IPv4)
            {
                this.hostIpEndPoint = new IPEndPoint(IPAddress.Parse(httpReq.Uri.Host), httpReq.Uri.Port);
            }
            // Host is DNS address
            else
            {
                // resolve Host name by DNS
                IPHostEntry hostEntry = Dns.GetHostEntry(httpReq.Uri.Host);
                // check for the first address not null
                // it seems that with .Net Micro Framework, the IPV6 addresses aren't supported and return "null"
                int i = 0;
                while (hostEntry.AddressList[i] == null) i++;
                this.hostIpEndPoint = new IPEndPoint(hostEntry.AddressList[i], httpReq.Uri.Port);
            }

            // add "Host" header
            httpReq.Host = httpReq.Uri.Host + ":" + httpReq.Uri.Port;
            // add "User-Agent" header
            httpReq.UserAgent = HTTP_CLIENT_NAME;

            // create socket and connect
            this.socket = new Socket(this.hostIpEndPoint.Address.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
            this.socket.Connect(this.hostIpEndPoint);

            // send HTTP request (request line and headers)
            this.buffer = Encoding.UTF8.GetBytes(httpReq.ToString());
            this.socket.Send(this.buffer);

            // raise event for allow client to send body request
            this.OnSendBody(httpReq);

            int received = 0;
            this.buffer = new byte[BUFFER_SIZE];
            HttpResponseParserResult result = HttpResponseParserResult.NotCompleted;
            HttpResponseParser parser = new HttpResponseParser();
            parser.ReceivingBody += parser_ReceivingBody;

            // receive on socket until parse is complete or no data received
            while ((result != HttpResponseParserResult.Completed) &&
                    this.socket.Poll(POLL_TIMEOUT, SelectMode.SelectRead))
            {
                // no data on the socket (closed or timeout)
                if (this.socket.Available == 0)
                    break;

                received = this.socket.Receive(this.buffer, BUFFER_SIZE, SocketFlags.None);
                result = parser.Parse(this.buffer, received);

                // if the HTTP response is malformed, break
                if (result == HttpResponseParserResult.Malformed)
                    break;
            }

            // unregister write callaback for body request
            httpReq.Body.Write = null;

            // close connection
            this.socket.Close();

            return parser.Response;
        }