Esempio n. 1
0
        private void RunRequest(Uri uri, Action <string> callback, ResponseType type)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

            req.Headers["UserAgent"]     = "ua_string=(PubNub-csharp/UnitTest)";
            req.Headers["Cache-Control"] = "no-cache";
            req.Headers["Pragma"]        = "no-cache";
            req.Timeout   = 310 * 1000;
            req.Pipelined = true;

            MyRequestState requestState = new MyRequestState();

            requestState.MyRequest = req;
            requestState.Callback  = callback;
            requestState.Type      = type;

            IAsyncResult asyncResult = req.BeginGetResponse(new AsyncCallback(UrlProcessResponseCallback), requestState);
        }
Esempio n. 2
0
        private void UrlProcessResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                // State of request is asynchronous.
                MyRequestState myRequestState   = (MyRequestState)asynchronousResult.AsyncState;
                HttpWebRequest myHttpWebRequest = myRequestState.MyRequest;
                using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult))
                {
                    string jsonString = "";
                    myRequestState.MyResponse = myHttpWebResponse;

                    Stream stream = myRequestState.MyResponse.GetResponseStream();
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        stream.Flush();
                        jsonString = streamReader.ReadToEnd();
                        streamReader.Close();
                        myRequestState.Callback(jsonString);
                    }
                    stream.Close();
                    System.Diagnostics.Debug.WriteLine(string.Format("@@ ** {0} ** Req Uri = {1} => JSON = {2}", myRequestState.Type.ToString(), myRequestState.MyResponse.ResponseUri, jsonString));
                    //System.Diagnostics.Debug.WriteLine("JSON = " + jsonString);
                }


                // Read the response into a Stream object.

                // Begin the Reading of the contents of the HTML page and print it to the console.
                //IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
                return;
            }
            catch (WebException e)
            {
                Console.WriteLine("\nRespCallback Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
        }
        private void RunRequest(Uri uri, Action<string> callback, ResponseType type)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            req.Headers["UserAgent"] = "ua_string=(PubNub-csharp/UnitTest)";
            req.Headers["Cache-Control"] = "no-cache";
            req.Headers["Pragma"] = "no-cache";
            req.Timeout = 310 * 1000;
            req.Pipelined = true;

            MyRequestState requestState = new MyRequestState();
            requestState.MyRequest = req;
            requestState.Callback = callback;
            requestState.Type = type;

            IAsyncResult asyncResult = req.BeginGetResponse(new AsyncCallback(UrlProcessResponseCallback), requestState);
            
        }