Csharp/C Sharp by API/System.Net/HttpWebRequest

Материал из .Net Framework эксперт
Версия от 12:13, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

HttpWebRequest.ContentLength

  
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( ));
      }
    }
  }
}


HttpWebRequest.ContentType

  
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( ));
      }
    }
  }
}


HttpWebRequest.Credentials

  
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( ));
      }
    }
  }
}


HttpWebRequest.GetResponse()

  
using System;
using System.Net;
using System.IO;
public class WebApp {
    public static void Main() {
        String page = "http://www.yoursite.net/index.html";
        HttpWebRequest site = (HttpWebRequest)WebRequest.Create(page);
        HttpWebResponse response =(HttpWebResponse)site.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader read = new StreamReader(dataStream);
        String data = read.ReadToEnd();
        Console.WriteLine(data);
    }
}


HttpWebRequest.Method

  
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( ));
      }
    }
  }
}