【GetData1】 【GetData2】 【GetData3】 【GetData4】 【GetData5】 【GetData6】 【GetData7】 【GetData8】 【GetData9】 【GetData10】 【GetData11】 【GetData12】 【GetData13】 【Ge ...
【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; }