Ejemplo n.º 1
0
        public string ExecuteRequest(WebClientInfo info)
        {
            var request = WebRequest.Create(info.RequestUri);

            request.Method = "PUT";

            var credentials = info.Credentials;

            if (credentials != null)
            {
                request.UseDefaultCredentials = false;
                request.Credentials           = credentials;
            }

            try
            {
                using (var stream = request.GetRequestStream())
                {
                    var writer = new StreamWriter(stream);
                    writer.Write(info.RequestText);
                    writer.Flush();
                }
            }
            catch (Exception ex)
            {
                return($"Error processing request: {ex.Message} ({ex.GetType().FullName})");
            }

            try
            {
                return(DumpResponse(request.GetResponse(), "response-data"));
            }
            catch (WebException ex)
            {
                return(DumpResponse(ex.Response, "response-error"));
            }
            catch (Exception ex)
            {
                return($"Error processing response: {ex.Message} ({ex.GetType().FullName})");
            }
        }
Ejemplo n.º 2
0
 public static WebClientInfo Create(object[] args)
 {
     try
     {
         var instance = new WebClientInfo();
         instance._requestUri = new Uri(args[0] as string);
         using (var reader = new StreamReader(args[1] as string))
         {
             instance._requestText = reader.ReadToEnd();
         }
         if (args.Length > 2)
         {
             instance._login = args[2] as string;
         }
         return(instance);
     }
     catch
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var info = WebClientInfo.Create(args);

            info.PromptForPassword += LocalPromptForUserValue;
            if (info == null)
            {
                Console.WriteLine("Usage: TestClient.exe <site URL> <JSON input file>");
            }
            else
            {
                var client = new LocalWebClient();
                Console.WriteLine("Web request input is");
                Console.WriteLine(info.RequestText);
                Console.WriteLine();
                Console.WriteLine($"Connecting to {info.RequestUri} ...");
                Console.WriteLine();
                Console.WriteLine(client.ExecuteRequest(info));
            }

            Console.WriteLine("Press any key to stop");
            Console.ReadKey();
        }