コード例 #1
0
        public HttpResponse SendRequest(String host, int port, String data)
        {
            int wait = 5;
            IPHostEntry entry = System.Net.Dns.GetHostEntry(host);
            HttpResponse response = new HttpResponse(this);

            if(entry != null)
            {
                IPAddress address = entry.AddressList[0];
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ep = new IPEndPoint(address, port);
                serverSocket.Connect(ep);
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                serverSocket.Send(buffer);
                StringBuilder line = new StringBuilder(1024);
                while (wait-- >= 0)
                {
                    int availableBytes = serverSocket.Available;
                    //Debug.Print(DateTime.Now.ToString() + " " + availableBytes.ToString() + " request bytes available");

                    int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);
                    if (bytesReceived > 0)
                    {
                        buffer = new byte[bytesReceived]; // Buffer probably should be larger than this.
                        int readByteCount = serverSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                        String contents = new String(Encoding.UTF8.GetChars(buffer));
                        line.Append(contents);
                    }
                    Thread.Sleep(100);
                }
                String[] lines = line.ToString().Split('\n');
                new HttpRequestParser().parse(null, response, new HttpRequestLines(lines));
                serverSocket.Close();
            }
            return response;
        }
コード例 #2
0
ファイル: HttpImpl.cs プロジェクト: jbuusao/NETMF-Projects
        public void parse(HttpRequest request, HttpResponse response, HttpRequestLines lines)
        {
            const String getVerb = "GET";
            const String postVerb = "POST";
            const String httpResponsePrefix = "HTTP/1.1 ";
            const String httpVerb = "HTTP/";
            const String hostVerb = "Host: ";
            const String connectionVerb = "Connection: ";
            const String acceptVerb = "Accept: ";
            const String userAgentVerb = "User-Agent: ";
            const String acceptEncodingVerb = "Accept-Encoding: ";
            const String acceptLanguageVerb = "Accept-Language: ";
            const String acceptCharsetVerb = "Accept-Charset: ";
            const String contentLengthVerb = "Content-Length: ";
            const String authorizationVerb = "Authorization: ";
            const String cookieVerb = "Cookie: ";
            int index;

            HttpBaseHeader header = null;
            if (request != null)
                header = request;
            else if (response != null)
                header = response;
            else throw new Exception("HttpRequestParser.parse: Parameter error");

            foreach (String received in lines)
            {
                Debug.Print("= " + received);

                if ((index = received.IndexOf(httpResponsePrefix)) == 0 && response != null)
                {
                    response.ErrorCode = received.Substring(index + httpResponsePrefix.Length).Trim();
                    continue;
                }

                if (request != null)
                {
                    if ((index = received.IndexOf(getVerb)) != -1)
                        request.RequestType = getVerb;
                    if ((index = received.IndexOf(getVerb)) != -1)
                        request.RequestType = getVerb;
                    else if ((index = received.IndexOf(postVerb)) != -1)
                        request.RequestType = postVerb;
                }

                if (index != -1)
                {
                    int spaceIndex = received.Substring(index).IndexOf(' ');
                    int httpIndex = received.IndexOf(httpVerb);
                    try
                    {
                        header.RawUrl = received.Substring(index + spaceIndex + 1, httpIndex - (index + spaceIndex) - 2);
                    }
                    catch (Exception)
                    {
                        Debug.Print("UNEXPECTED EXCEPTION on: " + received);
                    }
                }
                else if ((index = received.IndexOf(hostVerb)) != -1)
                {
                    header.Host = received.Substring(index + hostVerb.Length);
                }
                else if ((index = received.IndexOf(connectionVerb)) != -1)
                {
                    header.Connection = received.Substring(index + connectionVerb.Length);
                }
                else if ((index = received.IndexOf(acceptEncodingVerb)) != -1)
                {
                    header.AcceptEncoding = received.Substring(index + acceptEncodingVerb.Length);
                }
                else if ((index = received.IndexOf(acceptLanguageVerb)) != -1)
                {
                    header.AcceptLanguage = received.Substring(index + acceptLanguageVerb.Length);
                }
                else if ((index = received.IndexOf(acceptCharsetVerb)) != -1)
                {
                    header.AcceptCharSet = received.Substring(index + acceptCharsetVerb.Length);
                }
                else if ((index = received.IndexOf(acceptVerb)) != -1)
                {
                    header.Accept = received.Substring(index + acceptVerb.Length);
                }
                else if ((index = received.IndexOf(userAgentVerb)) != -1)
                {
                    header.UserAgent = received.Substring(index + userAgentVerb.Length);
                }
                else if ((index = received.IndexOf(authorizationVerb)) != -1)
                {
                    header.Authorization = received.Substring(index + authorizationVerb.Length);
                }
                else if ((index = received.IndexOf(cookieVerb)) != -1)
                {
                    header.RawCookies = received.Substring(index + cookieVerb.Length);
                }
                else if ((index = received.IndexOf(contentLengthVerb)) != -1)
                {
                    try
                    {
                        String l = received.Substring(index + contentLengthVerb.Length);
                        header.ContentLength = int.Parse(l);
                    }
                    catch (Exception e)
                    {
                        Debug.Print(e.ToString());
                    }
                }
                if (received.Length == 0)
                {
                    if (request != null && request.RequestType == "POST")
                    {
                        // We expect to see a line following this empty one
                        lines.MoveNext();
                        String postParams = (String)lines.Current;
                        request.parseContents(postParams);
                    }
                    else if (response != null)
                    {
                        // We expect to see a line following this empty one
                        int length = header.ContentLength;
                        if(length == 0)
                            length = 256;
                        StringBuilder sb = new StringBuilder(length + 10);
                        while (lines.MoveNext())
                        {
                            sb.Append(lines.Current.ToString());
                            sb.Append('\n');
                        }
                        response.Contents = sb.ToString();
                    }
                }
            }
        }