예제 #1
0
        // Return error code
        public int Post(string url, JsonTextWriter json)
        {
            WebRequest request = WebRequest.Create (serverURL+url);

              request.Method = "POST";

            json.AutoComplete();

            string text = json.ToString();

            Log.Write("Request: ");
            Log.WriteLine (text.ToString()+"\n");

            json.Close();

              byte[] postdata = System.Text.Encoding.ASCII.GetBytes (text);

            request.ContentLength = postdata.Length;

            // Wait one second when 60th RPC call is made during minute
            if (Interlocked.Read(ref throttle) == 60)
            {
                if (onMessage != null && severity >= 6)
                    onMessage(this,"[{0}] Throttling...", DateTime.Now);
                while (Interlocked.Read(ref throttle) == 60)
                    Thread.Sleep(1000);
            }

            if (timer.Enabled) Interlocked.Increment(ref throttle);

            try {
                Stream stream = request.GetRequestStream ();

              	stream.Write (postdata, 0, postdata.Length);
              	stream.Close ();
            } catch (System.Net.WebException ex) {
                if (onMessage != null && severity >= 3) onMessage(this,ex.Message);
                return -1;
            }

            if (onMessage != null && severity >= 7)
                onMessage(this,"\n[{0}] Connecting...",DateTime.Now);

            try {
              	WebResponse response = request.GetResponse ();
                ProcessResponse(response);
                if (onServerStatus != null) onServerStatus(this,new ServerStatusEventArgs());
            } catch (System.Net.WebException ex) {
                if (Regex.IsMatch(ex.Response.ContentType,"json-rpc")) {
                  WebResponse response = ex.Response;
                    ProcessResponse(response);
                    ServerErrorEventArgs args =
                        new ServerErrorEventArgs(r.error.code,r.error.data,r.error.message);
                    if (onServerError != null) onServerError(this,args);
                    return r.error.code;
                } else {
                    if (onMessage != null && severity >= 3) onMessage(this,ex.Message);
                    return -1;
                }
            }
            return 0;
        }
예제 #2
0
        private string Post(string url, JsonTextWriter json)
        {
            while (rpcCount > 50)
            { }//This creates the wait time until rpc count is reset.
            if (url == null) throw new Exception("URL is not assigned a value!");

            WebRequest request = WebRequest.Create(serverURL + url);
            request.Method = "POST";
            json.AutoComplete();
            string text = json.ToString();
            WriteStringToLog("request");
            WriteStringToLog(text);
//            log.WriteLine("Request");
//
            //Log.Write("Request: ");
            //Log.WriteLine(text.ToString() + "\n");

            json.Close();
            try
            {
            // Create a request using a URL that can receive a post. 
           // WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            //request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = text;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
                // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            //Console.WriteLine(responseFromServer);
            //Console.ReadKey();
            // Clean up the streams.
            WriteStringToLog("response");
            WriteStringToLog(responseFromServer);
           
            reader.Close();
            dataStream.Close();
            response.Close();
            rpcCount++;

            if (ServerResponseEvent != null)  //deserializes the server response and passes it out as an event.
            {
                JavaScriptSerializer js = new JavaScriptSerializer();  
                Response r = js.Deserialize<Response>(responseFromServer);
                if (r.id == 101)
                {
                    sessionID = r.result.session_id;
                    Console.WriteLine(sessionID);
                }

                ServerResponseEvent(this, r);
            }
            //log.WriteLine("Response");
            WriteStringToLog("Response");
            //log.WriteLine(responseFromServer);
            WriteStringToLog(responseFromServer);
            return responseFromServer;
            }
            catch
            {
                WriteStringToLog("Post function failure");
                //log.WriteLine("Post function failure");
                //log.Close();
                string failure = "failure";
                return failure;
            }

        }