/// <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); } }
/// <summary> /// Post /// </summary> /// <param name="uri"></param> /// <param name="postdata"></param> /// <returns></returns> public byte[] Post(URI uri,byte[] postdata) { Build(uri, postdata, eHttpMethod.POST); return Do(); }
/// <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);
/// <summary> /// Get /// </summary> /// <param name="uri"></param> /// <returns></returns> public byte[] Get(URI uri) { Build(uri, null, eHttpMethod.GET); return Do(); }