Csharp/C Sharp/Network/HTTP Put — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:46, 26 мая 2010

HTTP put with user name and password

using System;
using System.IO;
using System.Net;
public class HttpPut {
  public static void Main(string [] args) {
    string url = "http://www.nfex.ru/";
    string username = "user";
    string password = "password";
    string data = "This data should be written to the URL.";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "PUT";
    request.Credentials = new NetworkCredential(username, password);
    request.ContentLength = data.Length;
    request.ContentType = "text/plain";
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) {
      writer.WriteLine(data);
    }
    WebResponse response = request.GetResponse( );
    using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) {
      while (reader.Peek( ) != -1) {
        Console.WriteLine(reader.ReadLine( ));
      }
    }
  }
}