Ejemplo n.º 1
0
        } // End Sub MultipleDataSets

        public static void PostJSON(string url, System.Net.Http.HttpMethod method, object obj)
        {
            if (obj == null || obj == System.DBNull.Value)
            {
                throw new System.ArgumentNullException("obj");
            }
            // url = "http://localhost:53417/ajax/dataReceiver.ashx";

            Newtonsoft.Json.JsonSerializer sr = new Newtonsoft.Json.JsonSerializer();

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                client.Headers.Add("Content-Type", "application/json");

                using (System.IO.Stream postStream = client.OpenWrite(url, method.Method))
                {
                    sr.Formatting         = Newtonsoft.Json.Formatting.Indented;
                    sr.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
                    // sr.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified;

                    using (System.IO.TextWriter tw = new System.IO.StreamWriter(postStream, System.Text.Encoding.UTF8))
                    {
                        sr.Serialize(tw, obj);
                    } // End Using tw
                }     // End Using postStream
            }         // End Using client
        }             // End Sub PostJSON
 /// <summary>
 /// Publish the opml document to the specified location.
 /// </summary>
 /// <param name="uri">The URI of the resource to receive the data. </param>
 /// <param name="method">The method used to send the data to the resource. (POST)</param>
 /// <param name="proxy">HTTP proxy settings for the WebRequest class.</param>
 /// <param name="networkCredential">Credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.</param>
 /// <example>
 /// This sample shows how to publish (Default Proxy)
 /// <code>
 /// // password-based authentication for web resource
 /// System.Net.NetworkCredential providerCredential = new System.Net.NetworkCredential("username", "password", "domain");
 /// // use default system proxy
 /// Uri uri = new Uri("http://domain.net");
 /// Publish(uri, null, "POST", providerCredential);
 /// </code>
 /// This sample shows how to publish (Custom Proxy)
 /// <code>
 /// // password-based authentication for web resource
 /// System.Net.NetworkCredential providerCredential = new System.Net.NetworkCredential("username", "password", "domain");
 /// // password-based authentication for web proxy
 /// System.Net.NetworkCredential proxyCredential = new System.Net.NetworkCredential("username", "password", "domain");
 /// // create custom proxy
 /// System.Net.WebProxy webProxy = new System.Net.WebProxy("http://proxyurl:8080",false);
 /// webProxy.Credentials = proxyCredential;
 /// // publish
 /// Publish(uri, webProxy, "POST", providerCredential);
 /// </code>
 /// </example>
 public virtual void Publish(Uri uri, System.Net.WebProxy proxy, string method, System.Net.NetworkCredential networkCredential)
 {
     System.IO.Stream stream = null;
     try
     {
         // TODO: webproxy support
         System.Net.WebClient wc = new System.Net.WebClient();
         wc.Credentials = networkCredential;
         stream         = wc.OpenWrite(uri.AbsoluteUri, method);
         Save(stream);
     }
     finally
     {
         if (stream != null)
         {
             stream.Close();
         }
     }
 }
Ejemplo n.º 3
0
        public static void WriteDataToUrl(string uriString, string postData)
        {
            //string uriString;
            //System.Console.Write("\nPlease enter the URI to post data to : ");
            //uriString = System.Console.ReadLine();
            //System.Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:", uriString);
            //string postData = System.Console.ReadLine();

            byte[] postArray = System.Text.Encoding.UTF8.GetBytes(postData);

            System.Console.WriteLine("Uploading to {0} ...", uriString);

            // Create a new WebClient instance.
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                // client.OpenWriteCompleted
                // client.OpenReadCompleted


                // OpenWrite implicitly sets HTTP POST as the request method.
                // This method uses the STOR command to upload an FTP resource.
                // For an HTTP resource, the POST method is used.
                // client.OpenWrite("address", "POST");
                using (System.IO.Stream postStream = client.OpenWrite(uriString))
                {
                    postStream.Write(postArray, 0, postArray.Length);
                    // https://stackoverflow.com/questions/6779012/stream-with-openwrite-not-writing-until-it-is-closed
                    postStream.Flush();

                    // Close the stream and release resources.
                    postStream.Close();
                } // End Using postStream
            }     // End Using client


            System.Console.WriteLine("\nSuccessfully posted the data.");
        }
Ejemplo n.º 4
0
        public static void MultipleLargeDataSets()
        {
            System.Net.Http.HttpMethod meth = System.Net.Http.HttpMethod.Post;

            string fn = "lobster.json.txt";


            using (System.IO.Stream strm = new System.IO.FileStream(fn, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
            {
                MultipleLargeDataSets(strm, GetMultipleDataSetsSQL());
            } // End Using strm

            string endpointUrl = "http://localhost:53417/ajax/dataReceiver.ashx";

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                client.Headers.Add("Content-Type", "application/json");


                // client.OpenWriteCompleted += (sender, e) =>
                client.OpenWriteCompleted += delegate(object sender, System.Net.OpenWriteCompletedEventArgs e)
                {
                    // System.Net.WebClient that = (System.Net.WebClient) sender;

                    if (e.Error != null)
                    {
                        throw e.Error;
                    }

                    using (System.IO.Stream postStream = e.Result)
                    {
                        MultipleLargeDataSets(postStream, GetMultipleDataSetsSQL());
                        postStream.Flush();
                        postStream.Close();
                    }
                };

                client.OpenWriteAsync(new System.Uri(endpointUrl));


                using (System.IO.Stream postStream = client.OpenWrite(endpointUrl, meth.Method))
                {
                    // postStream.Write(fileContent, 0, fileContent.Length);
                    MultipleLargeDataSets(postStream, GetMultipleDataSetsSQL());
                } // End Using postStream

                using (System.IO.Stream postStream = client.OpenRead(endpointUrl))
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(postStream))
                    {
                        string output = sr.ReadToEnd();
                        System.Console.WriteLine(output);
                    } // End Using sr
                }     // End Using postStream


                // client.ResponseHeaders
            } // End Using client

            DataSetSerialization thisDataSet = EasyJSON.JsonHelper.DeserializeFromFile <DataSetSerialization>(fn);

            System.Console.WriteLine(thisDataSet);
        } // End Sub MultipleLargeDataSets