WCF学习笔记二:客户端调用控制台宿主程序_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > WCF学习笔记二:客户端调用控制台宿主程序

WCF学习笔记二:客户端调用控制台宿主程序

 2017/11/10 12:23:40  风之&诉  程序员俱乐部  我要评论(0)
  • 摘要:【GetData1】//请求(Ajax使用jsonp实现跨域访问)$.ajax({type:"get",url:"http://192.168.5.168:8123/Service1.svc/GetData1",data:{value:"张三"},dataType:"jsonp",success:function(data){alert(JSON.stringify(data));},error:function(error){alert('error');}});//结果{"value"
  • 标签:程序 笔记 学习 客户 客户端 WCF 学习笔记

【GetData1】

//请求(Ajax使用jsonp实现跨域访问)
$.ajax({
    type: "get",
    url: "http://192.168.5.168:8123/Service1.svc/GetData1",
    data: { value: "张三" },
    dataType: "jsonp",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (error) {
        alert('error');
    }
});

//结果        
{"value":"张三"}
//请求
string result = GetSend("http://192.168.5.168:8123/Service1.svc/GetData1", "value=张三");
//结果
{"value":"张三"}

 

【GetData2】

//请求(Ajax使用jsonp实现跨域访问)
$.ajax({
    type: "get",
    url: "http://192.168.5.168:8123/Service1.svc/GetData2",
    data: { cityid: "1001", cityname: "武汉市" },
    dataType: "jsonp",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (error) {
        alert('error');
    }
});

//结果        
{"CityID":"1001","CityName":"武汉市"}

 

//请求
string result = GetSend("http://192.168.5.168:8123/Service1.svc/GetData2", "cityid=1001&cityname=武汉市");
//结果
{"CityID":"1001","CityName":"武汉市"}

 

【GetData3】

//请求(Ajax使用jsonp实现跨域访问)
$.ajax({
    type: "get",
    url: "http://192.168.5.168:8123/Service1.svc/GetData3",
    data: { data: "{CityID:'1001',CityName:'江城'}" },
    dataType: "jsonp",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (error) {
        alert('error');
    }
});

//结果        
{"CityID":"1001","CityName":"旧:江城 -> 新:武汉"}
//请求
string result = GetSend("http://192.168.5.168:8123/Service1.svc/GetData3", "data={CityID:'1001',CityName:'江城'}");
//结果
{"CityID":"1001","CityName":"旧:江城 -> 新:武汉"}


【GetData4】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData4",
    type: "get",
    data: { name: "张三" },
    success: function (data) {
        alert(data);
    },
    error: function () {
        alert('error');
    }
});

//结果        
WCF服务,显示姓名:张三

 

//请求
string result = GetSend("http://192.168.5.168:8123/Service1.svc/GetData4", "name=张三");
//结果(注意:字符串两端有双引号)
"WCF服务,显示姓名:张三"

 

【GetData5】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData5",
    type: "get",
    data: { userID: "1001", userName: "张三" },
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserID":"1001","UserName":"张三"}

 

//请求
string result = GetSend("http://192.168.5.168:8123/Service1.svc/GetData5", "userID=1001&userName=张三");
//结果
{"UserID":"1001","UserName":"张三"}

 

【GetData6】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData6",
    type: "post",
    contentType: "text/json",
    data: '{"name":"张三"}',
    success: function (data) {
        alert(data);
    },
    error: function () {
        alert('error');
    }
});

//结果        
WCF服务,显示姓名:张三

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData6", "{\"name\":\"张三\"}");
//结果(注意:字符串两端有双引号)
"WCF服务,显示姓名:张三"

 

【GetData7】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData7",
    type: "post",
    contentType: "text/json",
    data: '{"userID":"1001","userName":"张三"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserID":"1001","UserName":"张三"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData7", "{\"userID\":\"1001\",\"userName\":\"张三\"}");
//结果
{"UserID":"1001","UserName":"张三"}

 

【GetData8】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData8",
    type: "post",
    contentType: "text/json",
    data: '{"UserName":"张三","Sex":"男"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserID":1000,"UserName":"张三","Sex":"男"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData8", "{\"UserName\":\"张三\",\"Sex\":\"男\"}");
//结果
{"UserID":1000,"UserName":"张三","Sex":""}

 

【GetData9】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData9",
    type: "post",
    contentType: "text/json",
    data: '{"UserName":"张三","Sex":"男"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserID":1000,"UserName":"张三","Sex":"男"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData9", "{\"UserName\":\"张三\",\"Sex\":\"男\"}");
//结果
{"UserID":1000,"UserName":"张三","Sex":""}

 

【GetData10】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData10",
    type: "post",
    contentType: "text/json",
    data: '{"Users":[{"UserID":1001,"UserName":"张三","Sex":"男"},{"UserID":1002,"UserName":"李四","Sex":"女"}]}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
[{"UserID":1001,"UserName":"张三","Sex":"男"},{"UserID":1002,"UserName":"李四","Sex":"女"}]

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData10", "{\"Users\":[{\"UserID\":1001,\"UserName\":\"张三\",\"Sex\":\"男\"},{\"UserID\":1002,\"UserName\":\"李四\",\"Sex\":\"女\"}]}");
//结果
[{"UserID":1001,"UserName":"张三","Sex":""},{"UserID":1002,"UserName":"李四","Sex":""}]

 

【GetData11】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData11",
    type: "post",
    contentType: "text/json",
    data: '{"Users":[{"UserID":1001,"UserName":"张三","Sex":"男"},{"UserID":1002,"UserName":"李四","Sex":"女"}]}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
[{"UserID":1001,"UserName":"张三","Sex":"男"},{"UserID":1002,"UserName":"李四","Sex":"女"}]

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData11", "{\"Users\":[{\"UserID\":1001,\"UserName\":\"张三\",\"Sex\":\"男\"},{\"UserID\":1002,\"UserName\":\"李四\",\"Sex\":\"女\"}]}");
//结果
[{"UserID":1001,"UserName":"张三","Sex":""},{"UserID":1002,"UserName":"李四","Sex":""}]

 

【GetData12】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData12",
    type: "post",
    contentType: "text/json",
    data: '{"UserID":"1001","UserName":"张三"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserID":"1001","UserName":"张三","CurrentTime":"2017-11-02 14:49:31"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData12", "{\"UserID\":\"1001\",\"UserName\":\"张三\"}");
//结果
{"UserID":"1001","UserName":"张三","CurrentTime":"2017-11-02 14:49:31"}

 

【GetData13】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData13",
    type: "post",
    contentType: "text/json",
    data: '{"UserID":"1001","UserName":"张三"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"Items":[{"__type":"Entity:#TalentCloud.Base.Entities","UserID":"1001","UserName":"张三","CurrentTime":"2017-11-03 09:35:02","UserAge":"男"},{"__type":"Entity:#TalentCloud.Base.Entities","UserID":"1002","UserName":"李四","CurrentTime":"2017-11-03 09:35:02","UserAge":"女"}],"Total":"0","Info":""}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData13", "{\"UserID\":\"1001\",\"UserName\":\"张三\"}");
//结果
{"Items":[{"__type":"Entity:#TalentCloud.Base.Entities","UserID":"1001","UserName":"张三","CurrentTime":"2017-11-03 09:35:02","UserAge":""},{"__type":"Entity:#TalentCloud.Base.Entities","UserID":"1002","UserName":"李四","CurrentTime":"2017-11-03 09:35:02","UserAge":""}],"Total":"0","Info":""}

 

【GetData14】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData14",
    type: "post",
    contentType: "text/json",
    data: '{"userID":"1001","userName":"张三"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserAge":"男","UserID":1001,"UserName":"张三"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData14", "{\"userID\":\"1001\",\"userName\":\"张三\"}");
//结果
{"UserAge":"","UserID":1001,"UserName":"张三"}

 

【GetData15】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData15",
    type: "post",
    contentType: "text/json",
    data: '{"UserID":"1001","UserName":"张三","UserAge":"男"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
{"UserAge":"男","UserID":1001,"UserName":"张三"}

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData15", "{\"UserID\":\"1001\",\"UserName\":\"张三\",\"UserAge\":\"男\"}");
//结果
{"UserAge":"","UserID":1001,"UserName":"张三"}

 

【GetData16】

//请求
$.ajax({
    url: "http://192.168.5.168:8123/Service1.svc/GetData16",
    type: "post",
    contentType: "text/json",
    data: '{"userID":"1001","userName":"张三"}',
    dataType: "json",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function () {
        alert('error');
    }
});

//结果        
[{"UserAge":"男","UserID":1001,"UserName":"张三"},{"UserAge":"女","UserID":1002,"UserName":"李四"}]

 

//请求
string result = PostJsonSend("http://192.168.5.168:8123/Service1.svc/GetData16", "{\"userID\":\"1001\",\"userName\":\"张三\"}");
//结果
[{"UserAge":"","UserID":1001,"UserName":"张三"},{"UserAge":"","UserID":1002,"UserName":"李四"}]

 

[客户端调用帮助方法]

public static string GetSend(string Url, string postDataStr)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
    request.Method = "GET";
    request.ContentType = "text/html;charset=UTF-8";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream myResponseStream = response.GetResponseStream();
    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
    string retString = myStreamReader.ReadToEnd();
    myStreamReader.Close();
    myStreamReader.Dispose();
    myResponseStream.Close();
    myResponseStream.Dispose();
    response.Close();
    request.Abort();

    return retString;
}

public static string PostJsonSend(string url,string data)
{
    string rs = null;
    ServicePointManager.DefaultConnectionLimit = 300;
    System.GC.Collect();
    CookieContainer cookieContainer = new CookieContainer();
    // 设置提交的相关参数
    HttpWebRequest request = null;
    HttpWebResponse SendSMSResponse = null;
    Stream dataStream = null;
    StreamReader SendSMSResponseStream = null;
    try
    {
        request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = false;
        request.ServicePoint.ConnectionLimit = 300;
        request.AllowAutoRedirect = true;
        request.Timeout = 90000;
        request.ReadWriteTimeout = 10000;
        request.ContentType = "application/json";
        request.Accept = "application/xml";
        //request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack"));
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        request.Proxy = null;
        request.CookieContainer = cookieContainer;

        using (dataStream = request.GetRequestStream())
        {
            dataStream.Write(bytes, 0, bytes.Length);
        }
        SendSMSResponse = (HttpWebResponse)request.GetResponse();
        if (SendSMSResponse.StatusCode == HttpStatusCode.RequestTimeout)
        {
            if (SendSMSResponse != null)
            {
                SendSMSResponse.Close();
                SendSMSResponse = null;
            }
            if (request != null)
            {
                request.Abort();
            }
            return null;
        }
        SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
        string strRespone = SendSMSResponseStream.ReadToEnd();

        return strRespone;
    }
    catch (Exception ex)
    {
        rs = ex.Message;
        if (dataStream != null)
        {
            dataStream.Close();
            dataStream.Dispose();
            dataStream = null;
        }
        if (SendSMSResponseStream != null)
        {
            SendSMSResponseStream.Close();
            SendSMSResponseStream.Dispose();
            SendSMSResponseStream = null;
        }
        if (SendSMSResponse != null)
        {
            SendSMSResponse.Close();
            SendSMSResponse = null;
        }
        if (request != null)
        {
            request.Abort();
        }
    }
    finally
    {
        if (dataStream != null)
        {
            dataStream.Close();
            dataStream.Dispose();
            dataStream = null;
        }
        if (SendSMSResponseStream != null)
        {
            SendSMSResponseStream.Close();
            SendSMSResponseStream.Dispose();
            SendSMSResponseStream = null;
        }
        if (SendSMSResponse != null)
        {
            SendSMSResponse.Close();
            SendSMSResponse = null;
        }
        if (request != null)
        {
            request.Abort();
        }
    }
    return rs;
}

 

发表评论
用户名: 匿名