WebClient.UploadData 方法

定义

将数据缓冲区上载到具有指定 URI 的资源。

重载

UploadData(String, Byte[])

将数据缓冲区上载到由 URI 标识的资源。

UploadData(Uri, Byte[])

将数据缓冲区上载到由 URI 标识的资源。

UploadData(String, String, Byte[])

使用指定的方法将数据缓冲区上载到指定资源。

UploadData(Uri, String, Byte[])

使用指定的方法将数据缓冲区上载到指定资源。

UploadData(String, Byte[])

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将数据缓冲区上载到由 URI 标识的资源。

public:
 cli::array <System::Byte> ^ UploadData(System::String ^ address, cli::array <System::Byte> ^ data);
public byte[] UploadData (string address, byte[] data);
member this.UploadData : string * byte[] -> byte[]
Public Function UploadData (address As String, data As Byte()) As Byte()

参数

address
String

接收数据的资源的 URI。

data
Byte[]

要发送到资源的数据缓冲。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

发送数据时出错。

- 或 -

承载资源的服务器没有响应。

示例

下面的代码示例将从控制台 Byte 输入的字符串转换为数组,并使用 UploadData将数组发布到指定的服务器。 服务器的任何响应都将显示在控制台。

Console::Write( "\nPlease enter the URI to post data to: " );
String^ uriString = Console::ReadLine();
// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
// Apply ASCII Encoding to obtain the String* as a Byte array.
array<Byte>^ postArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

//UploadData implicitly sets HTTP POST as the request method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, postArray );

// Decode and display the response.
Console::WriteLine( "\nResponse received was: {0}", Encoding::ASCII->GetString( responseArray ) );
    Console.Write("\nPlease enter the URI to post data to : ");
    string uriString = Console.ReadLine();
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
    string postData = Console.ReadLine();
    // Apply ASCII Encoding to obtain the string as a byte array.
    byte[] postArray = Encoding.ASCII.GetBytes(postData);
    Console.WriteLine("Uploading to {0} ...",  uriString);							
 myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

    //UploadData implicitly sets HTTP POST as the request method.
    byte[] responseArray = myWebClient.UploadData(uriString,postArray);

    // Decode and display the response.
    Console.WriteLine("\nResponse received was :{0}", Encoding.ASCII.GetString(responseArray));
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
' Apply ASCII Encoding to obtain the string as a byte array.
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' UploadData implicitly sets HTTP POST as the request method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, postArray)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

注解

方法 UploadData 将数据缓冲区发送到资源。

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。 如果服务器无法理解基础请求,则基础协议类将确定所发生的情况。 通常,会引发 , WebException 并将 Status 属性设置为 指示错误。

方法 UploadData 将 的内容 data 发送到服务器,而不对其进行编码。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadDataAsync 一。

BaseAddress如果 属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合在一起以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadData(Uri, Byte[])

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将数据缓冲区上载到由 URI 标识的资源。

public:
 cli::array <System::Byte> ^ UploadData(Uri ^ address, cli::array <System::Byte> ^ data);
public byte[] UploadData (Uri address, byte[] data);
member this.UploadData : Uri * byte[] -> byte[]
Public Function UploadData (address As Uri, data As Byte()) As Byte()

参数

address
Uri

接收数据的资源的 URI。

data
Byte[]

要发送到资源的数据缓冲。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

发送数据时出错。

- 或 -

承载资源的服务器没有响应。

注解

方法 UploadData 将数据缓冲区发送到资源。

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。 如果服务器无法理解基础请求,则基础协议类将确定所发生的情况。 通常,会引发 , WebException 并将 Status 属性设置为 指示错误。

方法 UploadData 将 的内容 data 发送到服务器,而不对其进行编码。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadDataAsync 一。

BaseAddress如果 属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合在一起以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadData(String, String, Byte[])

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将数据缓冲区上载到指定资源。

public:
 cli::array <System::Byte> ^ UploadData(System::String ^ address, System::String ^ method, cli::array <System::Byte> ^ data);
public byte[] UploadData (string address, string? method, byte[] data);
public byte[] UploadData (string address, string method, byte[] data);
member this.UploadData : string * string * byte[] -> byte[]
Public Function UploadData (address As String, method As String, data As Byte()) As Byte()

参数

address
String

接收数据的资源的 URI。

method
String

用于将数据发送到资源的 HTTP 方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

data
Byte[]

要发送到资源的数据缓冲。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

上载数据时出错。

- 或 -

承载资源的服务器没有响应。

示例

下面的代码示例将从控制台输入的字符串转换为字节数组,并使用 UploadData将数组发布到指定的服务器。 服务器的任何响应都将显示在控制台。

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

注解

方法 UploadData 使用 参数中指定的 method HTTP 方法将数据缓冲区发送到资源,并从服务器返回任何响应。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadDataAsync 一。

方法 UploadData 将 的内容 data 发送到服务器,而不对其进行编码。

method如果 参数指定了服务器无法理解的谓词,则基础协议类将确定所发生的情况。 通常,会引发 , WebException 并将 Status 属性设置为 指示错误。

BaseAddress如果 属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合在一起以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadData(Uri, String, Byte[])

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将数据缓冲区上载到指定资源。

public:
 cli::array <System::Byte> ^ UploadData(Uri ^ address, System::String ^ method, cli::array <System::Byte> ^ data);
public byte[] UploadData (Uri address, string? method, byte[] data);
public byte[] UploadData (Uri address, string method, byte[] data);
member this.UploadData : Uri * string * byte[] -> byte[]
Public Function UploadData (address As Uri, method As String, data As Byte()) As Byte()

参数

address
Uri

接收数据的资源的 URI。

method
String

用于将数据发送到资源的 HTTP 方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

data
Byte[]

要发送到资源的数据缓冲。

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

上载数据时出错。

- 或 -

承载资源的服务器没有响应。

注解

方法 UploadData 使用 参数中指定的 method HTTP 方法将数据缓冲区发送到资源,并从服务器返回任何响应。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadDataAsync 一。

方法 UploadData 将 的内容 data 发送到服务器,而不对其进行编码。

method如果 参数指定了服务器无法理解的谓词,则基础协议类将确定所发生的情况。 通常,会引发 , WebException 并将 Status 属性设置为 指示错误。

BaseAddress如果 属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合在一起以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于