WebClient.UploadString 方法

定义

将指定的字符串上载到指定的资源。

重载

UploadString(String, String)

使用 POST 方法将指定的字符串上载到指定的资源。

UploadString(Uri, String)

使用 POST 方法将指定的字符串上载到指定的资源。

UploadString(String, String, String)

使用指定的方法将指定的字符串上载到指定的资源。

UploadString(Uri, String, String)

使用指定的方法将指定的字符串上载到指定的资源。

UploadString(String, String)

使用 POST 方法将指定的字符串上载到指定的资源。

public:
 System::String ^ UploadString(System::String ^ address, System::String ^ data);
public string UploadString (string address, string data);
member this.UploadString : string * string -> string
Public Function UploadString (address As String, data As String) As String

参数

address
String

要接收字符串的资源的 URI。 对于 HTTP 资源,此 URI 必须标识可以接受用 POST 方法发送的请求的资源,如脚本或 ASP 页。

data
String

要上载的字符串。

返回

String

一个 String,包含服务器发送的响应。

例外

address 参数为 null

  • 或 -

data 参数为 null

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

  • 或 -

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

示例

下面的代码示例演示如何调用此方法。

void UploadString( String^ address )
{
   String^ data = "Time = 12:00am temperature = 50";
   WebClient^ client = gcnew WebClient;

   // Optionally specify an encoding for uploading and downloading strings.
   client->Encoding = System::Text::Encoding::UTF8;

   // Upload the data.
   String^ reply = client->UploadString( address, data );

   // Disply the server's response.
   Console::WriteLine( reply );
}
public static void UploadString(string address)
{
    string data = "Time = 12:00am temperature = 50";
    WebClient client = new WebClient();
    // Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8;
    // Upload the data.
    string reply = client.UploadString(address, data);
    // Display the server's response.
    Console.WriteLine(reply);
}
Public Shared Sub UploadString(ByVal address As String)

    Dim data As String = "Time = 12:00am temperature = 50"
    Dim client As WebClient = New WebClient()
    '  Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8
    '  Upload the data.
    Dim reply As String = client.UploadString(address, data)
    '  Disply the server's response.
    Console.WriteLine(reply)
End Sub

注解

在上传字符串之前,此方法使用属性中指定的Encoding编码将其Byte转换为数组。 此方法在传输字符串时阻止。 若要发送字符串并在等待服务器响应时继续执行,请使用其中 UploadStringAsync 一种方法。

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

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,将使用 POST 方法。

备注

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

适用于

UploadString(Uri, String)

使用 POST 方法将指定的字符串上载到指定的资源。

public:
 System::String ^ UploadString(Uri ^ address, System::String ^ data);
public string UploadString (Uri address, string data);
member this.UploadString : Uri * string -> string
Public Function UploadString (address As Uri, data As String) As String

参数

address
Uri

要接收字符串的资源的 URI。 对于 HTTP 资源,此 URI 必须标识可以接受用 POST 方法发送的请求的资源,如脚本或 ASP 页。

data
String

要上载的字符串。

返回

String

一个 String,包含服务器发送的响应。

例外

address 参数为 null

  • 或 -

data 参数为 null

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

  • 或 -

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

注解

在上传字符串之前,此方法使用属性中指定的Encoding编码将其Byte转换为数组。 此方法在传输字符串时阻止。 若要发送字符串并在等待服务器响应时继续执行,请使用其中 UploadStringAsync 一种方法。

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

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,将使用 POST 方法。

备注

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

适用于

UploadString(String, String, String)

使用指定的方法将指定的字符串上载到指定的资源。

public:
 System::String ^ UploadString(System::String ^ address, System::String ^ method, System::String ^ data);
public string UploadString (string address, string? method, string data);
public string UploadString (string address, string method, string data);
member this.UploadString : string * string * string -> string
Public Function UploadString (address As String, method As String, data As String) As String

参数

address
String

要接收字符串的资源的 URI。 此 URI 必须标识可以接受使用 method 方法发送的请求的资源。

method
String

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

data
String

要上载的字符串。

返回

String

一个 String,包含服务器发送的响应。

例外

address 参数为 null

  • 或 -

data 参数为 null

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

  • 或 -

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

  • 或 -

method 不能用来发送内容。

示例

下面的代码示例演示如何调用此方法。

void PostString( String^ address )
{
   String^ data = "Time = 12:00am temperature = 50";
   String^ method = "POST";
   WebClient^ client = gcnew WebClient;
   String^ reply = client->UploadString( address, method, data );
   Console::WriteLine( reply );
}
public static void PostString(string address)
{
    string data = "Time = 12:00am temperature = 50";
    string method = "POST";
    WebClient client = new WebClient();
    string reply = client.UploadString(address, method, data);

    Console.WriteLine(reply);
}
Public Shared Sub PostString(ByVal address As String)

    Dim data As String = "Time = 12:00am temperature = 50"
    Dim method As String = "POST"
    Dim client As WebClient = New WebClient()
    Dim reply As String = client.UploadString(address, method, data)

    Console.WriteLine(reply)
End Sub

注解

在上传字符串之前,此方法使用属性中指定的Encoding编码将其Byte转换为数组。 此方法在传输字符串时阻止。 若要发送字符串并在等待服务器响应时继续执行,请使用其中 UploadStringAsync 一种方法。

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

备注

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

适用于

UploadString(Uri, String, String)

使用指定的方法将指定的字符串上载到指定的资源。

public:
 System::String ^ UploadString(Uri ^ address, System::String ^ method, System::String ^ data);
public string UploadString (Uri address, string? method, string data);
public string UploadString (Uri address, string method, string data);
member this.UploadString : Uri * string * string -> string
Public Function UploadString (address As Uri, method As String, data As String) As String

参数

address
Uri

要接收字符串的资源的 URI。 此 URI 必须标识可以接受使用 method 方法发送的请求的资源。

method
String

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

data
String

要上载的字符串。

返回

String

一个 String,包含服务器发送的响应。

例外

address 参数为 null

  • 或 -

data 参数为 null

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

  • 或 -

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

  • 或 -

method 不能用来发送内容。

注解

在上传字符串之前,此方法使用属性中指定的Encoding编码将其Byte转换为数组。 此方法在传输字符串时阻止。 若要发送字符串并在等待服务器响应时继续执行,请使用其中 UploadStringAsync 一种方法。

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

备注

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

适用于