导出 (0) 打印
全部展开

HttpWebRequest.Method 属性

获取或设置请求的方法。

命名空间:System.Net
程序集:System(在 system.dll 中)

public override string Method { get; set; }
/** @property */
public String get_Method ()

/** @property */
public void set_Method (String value)

public override function get Method () : String

public override function set Method (value : String)

属性值

用于联系 Internet 资源的请求方法。默认值是 GET。

异常类型条件

ArgumentException

未提供任何方法。

- 或 -

方法字符串包含无效字符。

可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。

如果 ContentLength 属性被设置为 -1 以外的任何值,则必须将 Method 属性设置为上载数据的协议属性。

下面的代码示例将 Method 属性设置为 POST。

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


// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.set_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();
ubyte byte1[] = encoding.GetBytes(postData);

// Set the content type of the data being posted.
myHttpWebRequest.set_ContentType("application/x-www-form-"
    +"urlencoded");
// Set the content length of the string being posted.
myHttpWebRequest.set_ContentLength(postData.length());

Stream newStream = myHttpWebRequest.GetRequestStream();

newStream.Write(byte1, 0, byte1.length);
Console.WriteLine("The value of 'ContentLength' property after"
    + " sending the data is {0}", 
    System.Convert.ToString(myHttpWebRequest.get_ContentLength()));
// Close the Stream object.
newStream.Close();

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

社区附加资源

添加
显示:
© 2014 Microsoft