WebClient.UploadData 方法
定义
将数据缓冲区上载到具有指定 URI 的资源。Uploads a data buffer to a resource with the specified URI.
重载
| UploadData(String, Byte[]) |
将数据缓冲区上载到由 URI 标识的资源。Uploads a data buffer to a resource identified by a URI. |
| UploadData(Uri, Byte[]) |
将数据缓冲区上载到由 URI 标识的资源。Uploads a data buffer to a resource identified by a URI. |
| UploadData(String, String, Byte[]) |
使用指定的方法将数据缓冲区上载到指定资源。Uploads a data buffer to the specified resource, using the specified method. |
| UploadData(Uri, String, Byte[]) |
使用指定的方法将数据缓冲区上载到指定资源。Uploads a data buffer to the specified resource, using the specified method. |
UploadData(String, Byte[])
将数据缓冲区上载到由 URI 标识的资源。Uploads a data buffer to a resource identified by a 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。The URI of the resource to receive the data.
- data
- Byte[]
要发送到资源的数据缓冲。The data buffer to send to the resource.
返回
- Byte[]
一个 Byte 数组,它包含来自资源的响应的正文。A Byte array containing the body of the response from the resource.
例外
address 参数为 null。The address parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress, and address is invalid.
- 或 --or-
data 为 null。data is null.
- 或 --or-
发送数据时出错。An error occurred while sending the data.
- 或 --or-
承载资源的服务器没有响应。There was no response from the server hosting the resource.
示例
下面的代码示例将从控制台输入的字符串转换为 Byte 数组,并使用将数组发送到指定的服务器 UploadData 。The following code example converts a string entered from the console to a Byte array and posts the array to the specified server using UploadData. 服务器的任何响应都会显示到控制台。Any response from the server is displayed to the console.
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方法将数据缓冲区发送到资源。The UploadData method sends a data buffer to a resource.
此方法使用 STOR 命令上传 FTP 资源。This method uses the STOR command to upload an FTP resource. 对于 HTTP 资源,使用 POST 方法。For an HTTP resource, the POST method is used. 如果服务器不理解基础请求,则基础协议类将确定发生的情况。If the underlying request is not understood by the server, the underlying protocol classes determine what occurs. 通常, WebException 引发,并将 Status 属性设置为指示错误。Typically, a WebException is thrown with the Status property set to indicate the error.
UploadData方法将的内容发送 data 到服务器而不对其进行编码。The UploadData method sends the content of data to the server without encoding it. 上载数据时,此方法会阻止。This method blocks while uploading the data. 若要在等待服务器响应时继续执行,请使用其中一种 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.
适用于
UploadData(Uri, Byte[])
将数据缓冲区上载到由 URI 标识的资源。Uploads a data buffer to a resource identified by a 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。The URI of the resource to receive the data.
- data
- Byte[]
要发送到资源的数据缓冲。The data buffer to send to the resource.
返回
- Byte[]
一个 Byte 数组,它包含来自资源的响应的正文。A Byte array containing the body of the response from the resource.
例外
address 参数为 null。The address parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress, and address is invalid.
- 或 --or-
data 为 null。data is null.
- 或 --or-
发送数据时出错。An error occurred while sending the data.
- 或 --or-
承载资源的服务器没有响应。There was no response from the server hosting the resource.
注解
UploadData方法将数据缓冲区发送到资源。The UploadData method sends a data buffer to a resource.
此方法使用 STOR 命令上传 FTP 资源。This method uses the STOR command to upload an FTP resource. 对于 HTTP 资源,使用 POST 方法。For an HTTP resource, the POST method is used. 如果服务器不理解基础请求,则基础协议类将确定发生的情况。If the underlying request is not understood by the server, the underlying protocol classes determine what occurs. 通常, WebException 引发,并将 Status 属性设置为指示错误。Typically, a WebException is thrown with the Status property set to indicate the error.
UploadData方法将的内容发送 data 到服务器而不对其进行编码。The UploadData method sends the content of data to the server without encoding it. 上载数据时,此方法会阻止。This method blocks while uploading the data. 若要在等待服务器响应时继续执行,请使用其中一种 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.
适用于
UploadData(String, String, Byte[])
使用指定的方法将数据缓冲区上载到指定资源。Uploads a data buffer to the specified resource, using the specified method.
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。The URI of the resource to receive the data.
- method
- String
用于将数据发送到资源的 HTTP 方法。The HTTP method used to send the data to the resource. 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。If null, the default is POST for http and STOR for ftp.
- data
- Byte[]
要发送到资源的数据缓冲。The data buffer to send to the resource.
返回
- Byte[]
一个 Byte 数组,它包含来自资源的响应的正文。A Byte array containing the body of the response from the resource.
例外
address 参数为 null。The address parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress, and address is invalid.
- 或 --or-
data 为 null。data is null.
- 或 --or-
上载数据时出错。An error occurred while uploading the data.
- 或 --or-
承载资源的服务器没有响应。There was no response from the server hosting the resource.
示例
下面的代码示例将从控制台输入的字符串转换为字节数组,并使用将该数组发送到指定的服务器 UploadData 。The following code example converts a string entered from the console into a byte array and posts the array to the specified server using UploadData. 服务器的任何响应都会显示到控制台。Any response from the server is displayed to the console.
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方法使用参数中指定的 HTTP 方法将数据缓冲区发送到资源 method ,并返回来自服务器的任何响应。The UploadData method sends a data buffer to a resource using the HTTP method specified in the method parameter, and returns any response from the server. 上载数据时,此方法会阻止。This method blocks while uploading the data. 若要在等待服务器响应时继续执行,请使用其中一种 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.
UploadData方法将的内容发送 data 到服务器而不对其进行编码。The UploadData method sends the content of data to the server without encoding it.
如果 method 参数指定了服务器无法理解的谓词,则基础协议类将确定发生的情况。If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. 通常, WebException 引发,并将 Status 属性设置为指示错误。Typically, a WebException is thrown with the Status property set to indicate the error.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.
适用于
UploadData(Uri, String, Byte[])
使用指定的方法将数据缓冲区上载到指定资源。Uploads a data buffer to the specified resource, using the specified method.
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。The URI of the resource to receive the data.
- method
- String
用于将数据发送到资源的 HTTP 方法。The HTTP method used to send the data to the resource. 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。If null, the default is POST for http and STOR for ftp.
- data
- Byte[]
要发送到资源的数据缓冲。The data buffer to send to the resource.
返回
- Byte[]
一个 Byte 数组,它包含来自资源的响应的正文。A Byte array containing the body of the response from the resource.
例外
address 参数为 null。The address parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress, and address is invalid.
- 或 --or-
data 为 null。data is null.
- 或 --or-
上载数据时出错。An error occurred while uploading the data.
- 或 --or-
承载资源的服务器没有响应。There was no response from the server hosting the resource.
注解
UploadData方法使用参数中指定的 HTTP 方法将数据缓冲区发送到资源 method ,并返回来自服务器的任何响应。The UploadData method sends a data buffer to a resource using the HTTP method specified in the method parameter, and returns any response from the server. 上载数据时,此方法会阻止。This method blocks while uploading the data. 若要在等待服务器响应时继续执行,请使用其中一种 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.
UploadData方法将的内容发送 data 到服务器而不对其进行编码。The UploadData method sends the content of data to the server without encoding it.
如果 method 参数指定了服务器无法理解的谓词,则基础协议类将确定发生的情况。If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. 通常, WebException 引发,并将 Status 属性设置为指示错误。Typically, a WebException is thrown with the Status property set to indicate the error.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.