本文整理汇总了C#中System.Net.HttpWebRequest.Method属性的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.Method属性的具体用法?C# HttpWebRequest.Method怎么用?C# HttpWebRequest.Method使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。
在下文中一共展示了HttpWebRequest.Method属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: the
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :");
// Create a new string object to POST data to the Url.
string inputData = Console.ReadLine ();
string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);
// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;
Stream newStream = myHttpWebRequest.GetRequestStream ();
newStream.Write (byte1, 0, byte1.Length);
Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);
// Close the Stream object.
newStream.Close ();
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Net;
public class HttpPut {
public static void Main(string [] args) {
string url = "http://www.java2s.com/";
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( ));
}
}
}
}