Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1.  private static async Task Post()
  2.         {
  3.             HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://localhost:5000/setxml");
  4.             try
  5.             {
  6.                 request.Method = "POST";
  7.                 var data = System.Text.Encoding.UTF8.GetBytes("xml="+File.ReadAllText("file1.xml"));
  8.  
  9.                 request.ContentType = "application/x-www-form-urlencoded";
  10.                 request.ContentLength = data.Length;
  11.                 using (Stream dataStream = request.GetRequestStream())
  12.                 {
  13.                     dataStream.Write(data, 0, data.Length);
  14.                 }
  15.  
  16.                 var response = (HttpWebResponse) await request.GetResponseAsync();
  17.                 using (Stream stream = response.GetResponseStream())
  18.                 {
  19.                     using (StreamReader reader = new StreamReader(stream))
  20.                     {
  21.                         string line = "";
  22.                         while ((line = reader.ReadLine()) != null)
  23.                         {
  24.                             Console.WriteLine(line);
  25.                         }
  26.                     }
  27.                 }
  28.  
  29.                 response.Close();
  30.             }
  31.             catch (Exception e)
  32.             {
  33.                 Console.WriteLine(e);
  34.             }
  35.  
  36.  
  37.             Console.WriteLine("Запрос выполнен");
  38.             Console.Read();
  39.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement