当前位置: 代码迷 >> 综合 >> C# WebClient 发送post请求带参数
  详细解决方案

C# WebClient 发送post请求带参数

热度:116   发布时间:2023-11-21 05:54:12.0

C# WebClient 发送post请求带参数

string param= "参数1";
string param2= "参数2";
string[,] sensorNumList = new string[3,5] {
    {
     "0011", "0012", "0013", "0014", "0015"},{
     "0016", "0017", "0018", "0019", "001a"},{
     "001b", "001c", "001d", "001e", "001f"}};//二维数组参数
string url = ""; // api路径(请求接口)
string paramList = "{\"param1\":" + param+ ",\"param2\":\"" + param2+ "\,\"sensorNumList \":\"" + sensorNumList + "\"}";byte[] postData = Encoding.UTF8.GetBytes(paramList );WebClient webClient = new WebClient();webClient .Headers.Add("Content-Type", "application/json"); //采取POST方式必须加的headerwebClient .Headers.Add("ContentLength", postData.Length.ToString());byte[] responseData = client.UploadData(url, "POST", postData); //得到返回字符流Encoding.UTF8.GetString(responseData); //解码

  相关解决方案