日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

asp.net 與datasnap交互 Demo

 aaie_ 2013-12-04
基本數(shù)據(jù)
-->asp.net
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsFormsApplication1
{
    public class TJSONDataDemo
    {
        public TJSONDataDemo() { }
        public TResult[] result;
    }
    public class TResult
    {
        public string type;
        public Int32 id;
        public TRecList fields;
    }
    public class TRecList
    {
        public TRecObj[] MyRecList;
    }
    public class TRecObj
    {
        public string type;
        public Int32 id;
        public TFieldList fields;
    }
    public class TFieldList
    {
        //public Array[] Arr;
        public string Name;
        public Int32 Age;
        public double Birthday;
        public double Tall;
    }
    /*
    
     */
}
 
-->Delphi Xe2
type
  TMyRec= class
  private
    Name: string;
    Age: Integer;
    Birthday: TDateTime;
    Tall: Double;
  end;
  TMyClassDemo= class
  private
    MyRecList: array of TMyRec;
  end;
 
 
-->asp.net --->datasnap_rest
  #region "發(fā)收測試"

        /// <summary>
        /// 接收J(rèn)SON數(shù)據(jù)  并解析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //
            string ret = string.Empty;
            string paramData = string.Empty;
            Encoding dataEncode = Encoding.Default; ;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData); //轉(zhuǎn)化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(@"                //webReq.Method = "post";
                // webReq.ContentType = "application/x-www-form-urlencoded";
                //webReq.ContentType = "application/json";
                //webReq.ContentLength = byteArray.Length;
                //Stream newStream = webReq.GetRequestStream();
                //newStream.Write(byteArray, 0, byteArray.Length);//寫入?yún)?shù)
                //newStream.Close();
                webReq.UseDefaultCredentials = true;
                string username = "admin";
                string userPass = "admin";
                string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, userPass)));
                webReq.Headers.Add("Authorization", "Basic " + code);
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                TJSONDataDemo jDemo = JsonMapper.ToObject<TJSONDataDemo>(ret);
               
                textBox1.Text = ret;
                sr.Close();
                response.Close();
                //newStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 發(fā)送JSON數(shù)據(jù) Demo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string ret = string.Empty;
            string paramData = string.Empty;
            TRecList recList= new TRecList();
            recList.MyRecList= new TRecObj[3];
            for (int i = 0; i < recList.MyRecList.Length; i++)
            {
                recList.MyRecList[i] = new TRecObj();
                recList.MyRecList[i].id = i;
                recList.MyRecList[i].fields = new TFieldList();
                recList.MyRecList[i].fields.Age = i;
                recList.MyRecList[i].fields.Birthday = 12.22;
                recList.MyRecList[i].fields.Name = "姓名"+ i.ToString();
                recList.MyRecList[i].fields.Tall = 1223.33;
            }
            paramData = JsonMapper.ToJson(recList);
                //webReq.Credentials = CredentialCache.DefaultCredentials;
                webReq.UseDefaultCredentials = true;
                string username="admin";
                string userPass= "admin";
                string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, userPass)));
                webReq.Headers.Add("Authorization", "Basic " + code);
                /*
                  request.Credentials = CredentialCache.DefaultCredentials;
                //獲得用戶名密碼的Base64編碼
                string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password)));
                //添加Authorization到HTTP頭
                request.Headers.Add("Authorization", "Basic " + code);
                 */
               
               
                //webReq.AuthenticationLevel= System.Net.Security.AuthenticationLevel.
                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//寫入?yún)?shù)
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                //TJSONDataDemo jDemo = JsonMapper.ToObject<TJSONDataDemo>(ret);

                textBox1.Text = ret;
                sr.Close();
                response.Close();
                //newStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
 
-->Xe2_rest 服務(wù)器
...驗(yàn)證
procedure TServerContainer1.TDSAuthenticationManagerUserAuthenticate(Sender: TObject;
  const Protocol, Context, User, Password: string; var valid: Boolean;
  UserRoles: TStrings);
begin
  //
  if (User='admin') and (Password='admin') then
    valid:= true
  else valid:= False;
end;
 
...處理查詢
function TServerMethods1.JsonMarshallDemo: TJSONValue;
var
  myClsDemo: TMyClassDemo;
  i: Integer;
  jm: TJSONMarshal;
  jv: TJSONValue;
begin
  myClsDemo:= TMyClassDemo.Create;
  SetLength(myClsDemo.MyRecList,10);
  for i := 0 to 9 do begin
    myClsDemo.MyRecList[i]:= TMyRec.Create;
    myClsDemo.MyRecList[i].Name:= '姓名'+ IntToStr(i);
    myClsDemo.MyRecList[i].Age:= i;
    myClsDemo.MyRecList[i].Birthday:= now;
    myClsDemo.MyRecList[i].Tall:= i+ 1.2;
  end;
  jm:= TJSONMarshal.Create();
  jv:= jm.Marshal(myClsDemo);
  jm.Free;
  myClsDemo.Free;
  Result:= jv;
end;
 
 
...JSON提交
function TServerMethods1.updateJsonDemo(field: string;
  data: TJSONObject): string;
begin
  //
  Result:= field;

//  Result:= Result+ data
//  ;
  if data.Null then begin
    Result:= Result+ ' Data is null';
  end else begin
    Result:= Result+ ' data is not null';
    result:= Result+ data.ToString();
  end;
end;
 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多