zoukankan      html  css  js  c++  java
  • C# Post请求中Json格式写法

    C#

                try
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/CallCenter2/CallCenter2/Ajax_GridView_Select.php");
                    httpWebRequest.ContentType = "application/json;charset=utf8"; //;charset=UTF-8
                    httpWebRequest.Method = "POST";
    
                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        string json = "{"user":"李国林"," +
                                      ""password":"AA1122233444"}";
                        streamWriter.Write(json);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();   
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出错!"+ ex.Message);
                }

    PHP

    <?php 
    header("Content-Type:text/html;charset=utf8");
    
    require_once('Log/LogHelper.php');
    WriteLog('Ajax_GridView_Select---------');
    
    foreach($_POST as $k=>$v){    
        WriteLog("POST ". $k .'--' .$v);    
    }    
    foreach($_GET as $k=>$v){    
        WriteLog("GET ".  $k .'--' .$v);    
    }
    
    $json_request=file_get_contents("php://input");  //取请求过来的 JSON
    WriteLog( $json_request );
    $arr_request = json_decode($json_request,true);
    //输出var_dump($arr);
    WriteLog("arr_request0 " . $arr_request[0]);
    WriteLog("arr_request1 " . $arr_request[1]);
    
    $arr_result = array(); //返回值  
    
    $arr_result['rows'] = "EEEEEEEEE";     
    $arr_result['action'] = "黑名单"; 
    //echo json_encode($arr_result );
    echo encode_json($arr_result);
    
    
       //php 5.4版本之前json_encode的option里未添加JSON_UNESCAPED_UNICODE,需自定义函数处理
       function encode_json($str)
       {
           return urldecode(json_encode(url_encode($str)));
       }
       function url_encode($str)
       {
         if(is_array($str))
          {
              foreach($str as $key=>$value)
              {
                  $str[urlencode($key)] = url_encode($value);
              }
          }
          else
          {
              $str = urlencode($str);
          }
      
          return $str;
      }

    ?>
  • 相关阅读:
    PHP post接口返回数据
    wamp 安装多版本php
    关于WAMP的apache 人多了就访问非常卡的问题解决方法(转)
    在生产环境上重装wamp
    wamp不记录访问日志的办法
    oracle数据库锁表解决办法
    wampserver 中127.0.0.1可以访问,但localhost无法访问
    PLSQL中查询数据的时候查询结果显示中文乱码(转)
    Could not read from remote repository
    17-案例
  • 原文地址:https://www.cnblogs.com/hailexuexi/p/13346312.html
Copyright © 2011-2022 走看看