Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted

1 solution

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.

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


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


C#
//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();
        }


C#
// 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("", "");
        }


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 !!!
 
Share this answer
 
v2
Comments
kingsa 6-Oct-13 13:50pm    
Thhanks With gr8 coding and i have bunch of data like employess info in what data type i need to return to grid view can u guide i am strucking over here
Padam Agrawal 7-Oct-13 0:36am    
Return as a class object, like _GetInformation class you have to define own employee class with required members.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900