Beispiel #1
0
        //异步发超GET请求
        public bool HttpGet(string Url, string Para, AsyncHttpCallBack ACB)
        {
            if (!string.IsNullOrEmpty(Para))
            {
                Url = Url + "?" + Para;
            }

            HttpWebRequest Req = WebRequest.Create(Url) as HttpWebRequest;

            Req.Method = "Get";
            Req.ServicePoint.Expect100Continue = false;

            try
            {
                RequestState State = new RequestState();
                State.ACBack  = ACB;
                State.Request = Req;
                IAsyncResult result = (IAsyncResult)Req.BeginGetResponse(new AsyncCallback(ResponseCallback), State);

                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), Req, DefaultTimeout, true);

                WaitDone.WaitOne();

                State.Response.Close();
            }
            catch (Exception ex)
            {
                return(false);
            }


            return(true);
        }
Beispiel #2
0
 //构造函数
 public RequestState()
 {
     BufferRead     = new byte[BUFFER_SIZE];
     RequestData    = new StringBuilder("");
     Request        = null;
     StreamResponse = null;
     ACBack         = null;
 }
Beispiel #3
0
        //异步方式发起POST请求
        public bool HttpPost(string Url, string Para, AsyncHttpCallBack ACB)
        {
            StreamWriter Write = null;

            HttpWebRequest WebReq = WebRequest.Create(Url) as HttpWebRequest;

            WebReq.Method      = "POST";
            WebReq.ContentType = "application/x-www-form-urlencoded";
            WebReq.ServicePoint.Expect100Continue = false;
            try
            {
                Write = new StreamWriter(WebReq.GetRequestStream());
                Write.Write(Para);
                Write.Close();
                Write = null;

                RequestState State = new RequestState();
                State.ACBack  = ACB;
                State.Request = WebReq;

                IAsyncResult Result = (IAsyncResult)WebReq.BeginGetResponse(new AsyncCallback(ResponseCallback), State);
                ThreadPool.RegisterWaitForSingleObject(Result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), WebReq, DefaultTimeout, true);

                WaitDone.WaitOne();
                State.Response.Close();
            }
            catch (Exception ex)
            {
                if (Write != null)
                {
                    Write.Close();
                    Write = null;
                }
                return(false);
            }
            finally
            {
                if (Write != null)
                {
                    Write.Close();
                    Write = null;
                }
            }
            return(true);
        }
Beispiel #4
0
        public bool HttpPostFile(string Url, string Para, List <Parameter> Files, AsyncHttpCallBack AsyncCB)
        {
            Stream ReqStream = null;
            string Boundary  = DateTime.Now.Ticks.ToString("x");

            Url = Url + "?" + Para;
            HttpWebRequest WebReq = WebRequest.Create(Url) as HttpWebRequest;

            WebReq.Method = "POST";
            WebReq.ServicePoint.Expect100Continue = true;
            WebReq.ContentType = "multipart/form-data; boundary=" + Boundary;
            WebReq.KeepAlive   = true;
            WebReq.Credentials = CredentialCache.DefaultCredentials;
            try
            {
                Stream           MulStream    = new MemoryStream();
                byte[]           BoundaryByte = Encoding.ASCII.GetBytes("\r\n--" + Boundary + "\r\n");
                string           FormdataTem  = "\r\n--" + Boundary + "\r\nContent-Disposition:form-data:name=\"{0}\"\r\n\r\n{1}";
                List <Parameter> ListParm     = HttpUtility.GetQueryParameters(Para);

                foreach (Parameter P in ListParm)
                {
                    string FormItem     = string.Format(FormdataTem, P.Name, (FormParamDecode(P.Value)));
                    byte[] FormItemByte = Encoding.UTF8.GetBytes(FormItem);
                    MulStream.Write(FormItemByte, 0, FormItemByte.Length);
                }

                MulStream.Write(BoundaryByte, 0, BoundaryByte.Length);
                string HeaderTemp = "Content-Disposition:form-data:name=\"{0}\";filename=\"{1}\"\r\nContent-Type: \"{2}\"\r\n\r\n";

                foreach (Open.MingdaoSDK.Common.Parameter Pa in Files)
                {
                    string Name        = Pa.Name;
                    string FilePath    = Pa.Value;
                    string File        = Path.GetFileName(FilePath);
                    string ContentType = HttpUtility.GetContentType(File);

                    string Header      = string.Format(HeaderTemp, Name, File, ContentType);
                    byte[] HeaderBytes = Encoding.UTF8.GetBytes(Header);

                    MulStream.Write(HeaderBytes, 0, HeaderBytes.Length);

                    FileStream FileStr  = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                    byte[]     Buffer   = new byte[1024];
                    int        byteRead = 0;
                    while ((byteRead = FileStr.Read(Buffer, 0, Buffer.Length)) != 0)
                    {
                        MulStream.Write(Buffer, 0, byteRead);
                    }

                    MulStream.Write(BoundaryByte, 0, BoundaryByte.Length);

                    FileStr.Close();
                }

                WebReq.ContentLength = MulStream.Length;
                ReqStream            = WebReq.GetRequestStream();

                MulStream.Position = 0;
                byte[] TmpBuffer = new byte[MulStream.Length];
                MulStream.Read(TmpBuffer, 0, TmpBuffer.Length);
                MulStream.Close();
                ReqStream.Write(TmpBuffer, 0, TmpBuffer.Length);
                ReqStream.Close();
                ReqStream = null;

                RequestState RState = new RequestState();
                RState.ACBack  = AsyncCB;
                RState.Request = WebReq;

                IAsyncResult Result = (IAsyncResult)WebReq.BeginGetResponse(new AsyncCallback(ResponseCallback), RState);
                ThreadPool.RegisterWaitForSingleObject(Result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), WebReq, DefaultTimeout, true);
                WaitDone.WaitOne();

                RState.Response.Close();
            }
            catch (Exception ex)
            {
                if (ReqStream != null)
                {
                    ReqStream.Close();
                    ReqStream.Dispose();
                    ReqStream = null;
                }
                return(false);
            }
            finally
            {
                if (ReqStream != null)
                {
                    ReqStream.Close();
                    ReqStream.Dispose();
                    ReqStream = null;
                }
            }
            return(true);
        }