如何使用Url调用服务并传递多个参数 [英] How to cal the service using Url and passing multiple parametrs

查看:225
本文介绍了如何使用Url调用服务并传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用json格式的uri调用wcf

我有以下服务我想用.net调用这个服务它返回字符串可以指导我或使用休息服务发送片段



[OperationContract]

[WebInvoke(Method =GET,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate =/?Username = {Username}&Password = {Password})]

String LoginUers(String Username,String Password);

Hi ,
Iiwant to call wcf using uri in json format
I have below service i want to call this service using .net it returns string can u guide me or send snippets using rest service

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/?Username={Username}&Password={Password}")]
String LoginUers(String Username, String Password);

推荐答案



当你使用GET方法时,你必须为特殊字符给予很多处理,更好地使用POST方法,并且也有助于安全问题,请尝试以下逻辑将帮助你。



Hi,
When you will go with GET method you have to give a lot treatment for special chars, better use POST method and would help for security concern too, try below logic will help you out.

//define class
public class _GetInformation
{
        public string UserEmail;
}







// signature POST method
[OperationContract]
[WebInvoke(
            Method = "POST",
            UriTemplate = "Get_Information"
           )
        ]
        string Get_Information(Stream data);







//Implement
public string Get_Information(Stream data) 
        {
            StringBuilder _JsonGet_Information = new StringBuilder();
            string UserEmail = string.Empty;
            _GetInformation Obj_GetInformation;

      DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(_GetInformation));

                Obj_GetInformation = obj.ReadObject(data) as _GetInformation;

                UserEmail = Obj_GetInformation.UserEmail;

                return UserEmail.ToString();
        }







// How to make web request or call post method
        public void CallPostMethod()
        {
            
            // declare ascii encoding
            ASCIIEncoding encoding = new ASCIIEncoding();
            string strResult = string.Empty;

            // sample xml sent to Service & this data is sent in POST
            string SampleXml = "" ;

            _GetInformation oList = new _GetInformation {UserEmail = "mdelgado"};
            
            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string sJSON = oSerializer.Serialize(oList);

            SampleXml = sJSON;

            string postData = SampleXml.ToString();

            // convert xmlstring to byte using ascii encoding
            byte[] data = encoding.GetBytes(postData);

            // Restful service URL
            string url = "http://localhost/my_API/RestServiceImpl.svc/Get_Information"; 

            // declare httpwebrequet wrt url defined above
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            // set method as post
            webrequest.Method = "POST";

            // set content type
            webrequest.ContentType = "application/x-www-form-urlencoded";

            // set content length
            webrequest.ContentLength = data.Length;
            // get stream data out of webrequest object
            Stream newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            // declare & read response from service
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

            // set utf8 encoding
            Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
            // read response stream from response object
            StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
            // read string from stream data
            strResult = loResponseStream.ReadToEnd();
            // close the stream object
            loResponseStream.Close();
            // close the response object
            webresponse.Close();
            // below steps remove unwanted data from response string
            strResult = strResult.Replace("", "");
        }





请记住这是示例代码只是给你一个想法_GetInformation将是保持返回数据的类。你需要定制它。



祝你有美好的一天!!!



Remember this is sample code just give you idea _GetInformation would be class that kept returning data. you need to customized it.

Have a great day !!!


这篇关于如何使用Url调用服务并传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆