Пример #1
0
 public void SetMethod(eHttpMethod method)
 {
     this.Method = method;
 }
Пример #2
0
        /// <summary>
        /// Do
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="content"></param>
        /// <param name="method"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public void Build(URI uri,byte[] content,eHttpMethod method,IDictionary<string,string> headers)
        {
            //确保HTTP
            if (uri.Scheme=="https")
            {
                throw new NotImplementedException("Not Implemented Https");
            }
            else if (uri.Scheme!="http")
            {
                throw new ArgumentException("Error Scheme");
            }


            //获取IP
            IPAddress ip;
            try
            {
                ip = DNSHelper.GetHostIPV4Address(uri.Host);
            }
            catch
            {
                throw new ArgumentException("Error Host");
            }


            this.m_socket = new TcpSocket();
            int port = uri.Port;
            try
            {
                //连接
                m_socket.Connect(ip, port);
            }
            catch
            {
                throw;
            }



            this.stream = new NetworkStream(m_socket, true);

            //生成HttpRequest
            request = new HttpRequestForClient();
            request.SetMethod(method);
            request.SetURI(uri);
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            if(content!=null)
            {
                request.Write(content);
            }
            
        }
Пример #3
0
 public string GetResponseString(Uri RequestUri, eHttpMethod method, string postbytes) => GetResponseBytes(RequestUri, method, postbytes.ConvertToBytes()).ConvertFromBytes();
Пример #4
0
 /// <summary>
 /// Do
 /// </summary>
 /// <param name="uri"></param>
 /// <param name="content"></param>
 /// <param name="method"></param>
 /// <returns></returns>
 public void Build(URI uri, byte[] content, eHttpMethod method) => Build(uri, content, method, null);
Пример #5
0
        public byte[] GetResponseBytes(Uri RequestUri, eHttpMethod method, byte[] postbytes)
        {
            if (uri == null || !uri.Equals(RequestUri))
            {
                this.uri = RequestUri;
            }
            if (socket == null)
            {
                socket = new TcpSocket();
            }
            if (method != eHttpMethod.GET && method != eHttpMethod.POST)
            {
                throw new Exception("Unsupport Method");
            }
            if (!uri.IsAbsoluteUri)
            {
                throw new Exception("Absolute Uri is required");
            }
            if (uri.Scheme != "http")
            {
                throw new Exception("Unsupport protocol");
            }
            var request = new HttpRequest();
            request.Method = method;
            request.uri = this.uri.PathAndQuery;
            request.headers[eHttpRequestHeader.Host] = this.uri.Host;
            request.headers[eHttpRequestHeader.Connection] = "keep-alive";
            request.Cookies = this.Cookies;
            //request.headers[eHttpRequestHeader.AcceptEncoding] = "gzip;q=1.0";
            if (request.Method == eHttpMethod.POST)
            {
                if (postbytes == null)
                {
                    throw new ArgumentNullException("postbytes cannot be null");
                }
                request.headers[eHttpRequestHeader.ContentLength] = postbytes.Length.ToString();
                request.headers[eHttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
                request.Write(postbytes);
            }
            var data = request.GetAll();
            try
            {
                if(!connected)
                {
                    socket.Connect(Dns.GetHostAddresses(uri.DnsSafeHost)[0], this.uri.Port);
                    connected = true;
                }
                socket.Send(data);
                var response = new HttpResponse(Cookies);
                while (!response.IsComplete)
                {
                    while (socket.IsDataAvailable())
                    {
                        byte[] buffer = new byte[8 * 1024];
                        socket.Receive(buffer);
                        response.Read(buffer);
                    }
                }
                this.Cookies = response.Cookies;
                if (response.headers.Connection == eConnectionType.Close)
                {
                    socket = null;
                }
                return response.GetContent();

            }
            catch(Exception e)
            {
                Log.Log.Default.Error(e);
            }
            return new byte[] { };
        }