Exemplo n.º 1
0
        /// <summary>
        /// 开始使用WebService
        /// </summary>
        /// <param name="key"></param>
        /// <param name="callback"></param>
        public static void FunctionService(string key, Action <string> callback)
        {
            //功能
            AsyncState state = new AsyncState(key, callback, "", null);

            state.serviceInvoker.BeginFunction(key, 0, new AsyncCallback(EndFunctionService), state);
        }
Exemplo n.º 2
0
        /// <summary>
        /// WebService调用异步回调函数
        /// </summary>
        /// <param name="r"></param>
        private static void EndFunctionService(IAsyncResult r)
        {
            AsyncState state = r.AsyncState as AsyncState;
            string     res   = state.serviceInvoker.EndFunction(r);

            if (res == null)
            {
                state.serviceInvoker.BeginFunction(state.key, 0, new AsyncCallback(EndFunctionService), state);
            }
            else if (state.WSdele != null)
            {
                state.WSdele(res);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 开始请求
 /// </summary>
 /// <param name="state"></param>
 private static void AsyncRequest(AsyncState state)
 {
     try
     {
         state.r.BeginGetRequestStream(new AsyncCallback(RequestCallBack), state);
     }
     catch
     {
         if (state.dele != null)
         {
             state.dele(null);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 开始请求响应
 /// </summary>
 /// <param name="state"></param>
 private static void AsyncResponse(AsyncState state)
 {
     try
     {
         state.r.BeginGetResponse(new AsyncCallback(ResponseCallBack), state);
     }
     catch
     {
         if (state.dele != null)
         {
             state.dele(null);
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Web请求异步回调函数
        /// </summary>
        /// <param name="r"></param>
        private static void RequestCallBack(IAsyncResult r)
        {
            AsyncState state = r.AsyncState as AsyncState;

            try
            {
                Stream s = state.r.EndGetRequestStream(r);
                s.Write(state.data, 0, state.data.Length);
                s.Close();
                AsyncResponse(state);
            }
            catch
            {
                if (state.dele != null)
                {
                    state.dele(null);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Web响应异步回调函数
        /// </summary>
        /// <param name="r"></param>
        private static void ResponseCallBack(IAsyncResult r)
        {
            Stream     s     = null;
            AsyncState state = r.AsyncState as AsyncState;

            try
            {
                s = (state.r.EndGetResponse(r) as HttpWebResponse).GetResponseStream();
            }
            catch { }
            if (state.dele != null)
            {
                state.dele(s);
            }
            if (s == null)
            {
                return;
            }
            s.Close();
        }
Exemplo n.º 7
0
 /// <summary>
 /// 开始使用WebService
 /// </summary>
 /// <param name="key"></param>
 /// <param name="callback"></param>
 public static void FunctionService(string key, Action<string> callback)
 {
     //功能
     AsyncState state = new AsyncState(key, callback, "", null);
     state.serviceInvoker.BeginFunction(key, 0, new AsyncCallback(EndFunctionService), state);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 向指定url发送一段数据,并等待异步返回
 /// </summary>
 /// <param name="url"></param>
 /// <param name="data"></param>
 /// <param name="method"></param>
 /// <param name="dele"></param>
 public static HttpWebResponse Request(string url, NameValueCollection data, RequestMethod method, Action<Stream> dele, CookieContainer cc, string referer)
 {
     StringBuilder query = new StringBuilder();
     byte[] postdata = null;
     if (data != null)
     {
         for (int i = 0; i < data.Count; i++)
         {
             query.Append(HttpUtility.UrlEncode(data.GetKey(i), Encoding.UTF8));
             query.Append("=");
             query.Append(HttpUtility.UrlEncode(data[i], Encoding.UTF8));
             if (i != data.Count - 1)
                 query.Append("&");
         }
         switch (method)
         {
             case RequestMethod.Get:
                 Uri uri = new Uri(url);
                 url = string.Format("{0}?{1}", uri.AbsolutePath, query);
                 if (!string.IsNullOrEmpty(uri.Query))
                     url = string.Format("{0}&{1}", url, uri.Query);
                 break;
             case RequestMethod.Post:
                 postdata = Encoding.ASCII.GetBytes(query.ToString());
                 break;
         }
     }
     AsyncState state = new AsyncState(postdata, dele, url, method, cc);
     state.r.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10";
     state.r.Referer = referer;
     state.r.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
     state.r.AllowAutoRedirect = true;
     state.r.KeepAlive = true;
     if (dele == null)
     {
         if (state.data != null)
         {
             state.r.ContentLength = state.data.Length;
             Stream s = state.r.GetRequestStream();
             s.Write(state.data, 0, state.data.Length);
             s.Close();
         }
         return state.r.GetResponse() as HttpWebResponse;
     }
     else
     {
         if (state.data != null)
             AsyncRequest(state);
         else
             AsyncResponse(state);
     }
     return null;
 }
Exemplo n.º 9
0
 /// <summary>
 /// 开始请求
 /// </summary>
 /// <param name="state"></param>
 private static void AsyncRequest(AsyncState state)
 {
     try
     {
         state.r.BeginGetRequestStream(new AsyncCallback(RequestCallBack), state);
     }
     catch
     {
         if (state.dele != null)
             state.dele(null);
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// 开始请求响应
 /// </summary>
 /// <param name="state"></param>
 private static void AsyncResponse(AsyncState state)
 {
     try
     {
         state.r.BeginGetResponse(new AsyncCallback(ResponseCallBack), state);
     }
     catch
     {
         if (state.dele != null)
             state.dele(null);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// 向指定url发送一段数据,并等待异步返回
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="method"></param>
        /// <param name="dele"></param>
        public static HttpWebResponse Request(string url, NameValueCollection data, RequestMethod method, Action <Stream> dele, CookieContainer cc, string referer)
        {
            StringBuilder query = new StringBuilder();

            byte[] postdata = null;
            if (data != null)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    query.Append(HttpUtility.UrlEncode(data.GetKey(i), Encoding.UTF8));
                    query.Append("=");
                    query.Append(HttpUtility.UrlEncode(data[i], Encoding.UTF8));
                    if (i != data.Count - 1)
                    {
                        query.Append("&");
                    }
                }
                switch (method)
                {
                case RequestMethod.Get:
                    Uri uri = new Uri(url);
                    url = string.Format("{0}?{1}", uri.AbsolutePath, query);
                    if (!string.IsNullOrEmpty(uri.Query))
                    {
                        url = string.Format("{0}&{1}", url, uri.Query);
                    }
                    break;

                case RequestMethod.Post:
                    postdata = Encoding.ASCII.GetBytes(query.ToString());
                    break;
                }
            }
            AsyncState state = new AsyncState(postdata, dele, url, method, cc);

            state.r.UserAgent         = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10";
            state.r.Referer           = referer;
            state.r.Accept            = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            state.r.AllowAutoRedirect = true;
            state.r.KeepAlive         = true;
            if (dele == null)
            {
                if (state.data != null)
                {
                    state.r.ContentLength = state.data.Length;
                    Stream s = state.r.GetRequestStream();
                    s.Write(state.data, 0, state.data.Length);
                    s.Close();
                }
                return(state.r.GetResponse() as HttpWebResponse);
            }
            else
            {
                if (state.data != null)
                {
                    AsyncRequest(state);
                }
                else
                {
                    AsyncResponse(state);
                }
            }
            return(null);
        }