使用WebClient发送POST请求

  /// <summary>
    
/// 发送post请求
    
/// </summary>
    
/// <param name="url">请求的url</param>
    
/// <param name="postString">发送到数据 例如:"name=xhan&password=1231"</param>
    
/// <param name="encoding">发送和接受数据使用的编码</param>
    
/// <returns>服务器响应字符串</returns>
    public static string SendPostRequest(string url, string postString,Encoding encoding)
    {
        
byte[] postData = encoding.GetBytes(postString);

        WebClient client 
= new WebClient();
        client.Headers.Add(
"Content-Type""application/x-www-form-urlencoded");
        client.Headers.Add(
"ContentLength", postData.Length.ToString());

        
byte[] responseData = client.UploadData(url, "POST", postData);
        
return encoding.GetString(responseData);
    }
原文地址:https://www.cnblogs.com/xhan/p/1519071.html