none
Web Services to Handle JSON Objects RRS feed

  • Question

  • Hi all,

    I have a couple of Questions regarding Web Services and their Usage in ASP.NET

    1. Will Web Services respond with Http Messages?

    2. Will they be able to handle JSON Objects? if yes, can someone post a Tutorial on how to do this or share their knowledge?

    3. What if I want to create a Web Service and host it on my Console Application that can receive Http Requests and respond with a Http Response? How to do it?

    Kindly clarify these questions of mine.

    Thanks,


    K.V.N.PAVAN

    Tuesday, May 27, 2014 5:42 AM

Answers

  • Hello,

    Here, I am trying to address your query.

    1) if you need Http messages in response, the best way is to create Http handlers instead of Web Service.

    Below code snippet gives an idea, how to create http handler which returns Http response back.

    public class Download : IHttpHandler, IRequiresSessionState {
    
        public void ProcessRequest(HttpContext context) {
        	var pdfBytes = /* load the file here */
        	context.Response.ContentType = @"Application/pdf";
        	context.Response.BinaryWrite(pdfBytes);
        	context.Response.End();
        }
    }
    

    2) Yes, We can handle JSON object in Http web service. Below is example articles which provides basic code snippet about how to achieve this.

    Fundamental Information :

    http://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx

    Code snippet

    http://www.aspsnippets.com/Articles/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet.aspx

    3) Yes, it is possible to host http web service on console application. You can use WebServiceHost class to host webserive on console.

    Below MSDN article provide you information and code snippet about how to do this.

    http://msdn.microsoft.com/en-us/library/bb412178(v=vs.110).aspx

    Hope, this will answer your queries. Feel free to write in case further information is required.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Tuesday, May 27, 2014 6:32 AM
  • Hi,

    It's an obvious, it is socket server which uses TCP/IP to pass messages, so in that case HttpContext.current would be always null.

    What you can do here is let it be Multithreaded Socket server which just receive request, based on the request pass your request to appropriate Handlers (IHttpHandler) and just response back over TCP/IP socket whatever response you received from Handlers. Yes, for that you need to develop one another project which comprises of Handlers and there may be hosted on either another port of same server or on IIS.

    Hope, this will answer your query.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Wednesday, May 28, 2014 4:35 AM

All replies

  • Hello,

    Here, I am trying to address your query.

    1) if you need Http messages in response, the best way is to create Http handlers instead of Web Service.

    Below code snippet gives an idea, how to create http handler which returns Http response back.

    public class Download : IHttpHandler, IRequiresSessionState {
    
        public void ProcessRequest(HttpContext context) {
        	var pdfBytes = /* load the file here */
        	context.Response.ContentType = @"Application/pdf";
        	context.Response.BinaryWrite(pdfBytes);
        	context.Response.End();
        }
    }
    

    2) Yes, We can handle JSON object in Http web service. Below is example articles which provides basic code snippet about how to achieve this.

    Fundamental Information :

    http://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx

    Code snippet

    http://www.aspsnippets.com/Articles/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet.aspx

    3) Yes, it is possible to host http web service on console application. You can use WebServiceHost class to host webserive on console.

    Below MSDN article provide you information and code snippet about how to do this.

    http://msdn.microsoft.com/en-us/library/bb412178(v=vs.110).aspx

    Hope, this will answer your queries. Feel free to write in case further information is required.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Tuesday, May 27, 2014 6:32 AM
  • Hi Tejas,

    Thanks for your reply. 

    The first approach seems good for me as I already have a Console Application. But in there I am not using any Ihandlers etc.. I just wrote a ServerSocket listening on a port say 10010 and from client side I created a HttpWebRequest object and called the getResponse method of that object. The Server socket is able to receive the connection and request but not sure on how to return it. Here is the code snipet from Server side from where I expect a Http Response to Client's getresponse method call. Couls you suggest change to my code below? as you can see I am handling everything myself about http request received. Also in mycase I don't know why HttpContext.Current is always null

     private void Handler()
            {
                bool recvRequest = true;
                string EOL = "\r\n";
                int bodylength = 0;
                string requestPayload = "";
                string requestTempLine = "";
                List<string> requestLines = new List<string>();
                byte[] requestBuffer = new byte[1];
                byte[] responseBuffer = new byte[1];
                requestLines.Clear();
    
                try
                {
                    //State 0: Handle Request from Client
                    while (recvRequest)
                    {
                        while (this.clientSocket.Available != 0)
                        {
                            this.clientSocket.Receive(requestBuffer);
                            string fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
                            requestPayload += fromByte;
                            requestTempLine += fromByte;
    
                            if (requestTempLine.EndsWith(EOL))
                            {
                                requestLines.Add(requestTempLine.Trim());
                                requestTempLine = "";
                            }
    
                            if (requestPayload.Contains(EOL + EOL))
                            {
                                string[] list = requestLines.ToArray();
                                string body = "";
                                foreach (string s in list)
                                {
                                    if (s.Contains("Content-Length"))
                                    {
                                        bodylength = Convert.ToInt32(s.Split(':')[1]);
                                        for (int i = 0; i < bodylength; i++)
                                        {
                                            this.clientSocket.Receive(requestBuffer);
                                            fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
                                            body += fromByte;
                                        }
                                        requestPayload += body;
                                        requestLines.Add(body);
    
                                        AccountNo = Convert.ToInt64(body.Split('&')[0].Split('=')[1]);
                                        Amount = Convert.ToInt64(body.Split('&')[1].Split('=')[1]);
                                    }
                                }
                                recvRequest = false;
                            }
                        }
                    }
                    Console.WriteLine("Raw Request Received...as \n {0}",requestPayload);
           

    It is a Multithreaded Server listening on Socket 10010 and the above code is a handler for a socket connection. Can you let me know where or how to modify this?

    Thanks,


    K.V.N.PAVAN



    • Edited by Pa1 Kumar Tuesday, May 27, 2014 10:13 PM
    Tuesday, May 27, 2014 3:19 PM
  • Hi,

    It's an obvious, it is socket server which uses TCP/IP to pass messages, so in that case HttpContext.current would be always null.

    What you can do here is let it be Multithreaded Socket server which just receive request, based on the request pass your request to appropriate Handlers (IHttpHandler) and just response back over TCP/IP socket whatever response you received from Handlers. Yes, for that you need to develop one another project which comprises of Handlers and there may be hosted on either another port of same server or on IIS.

    Hope, this will answer your query.


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Wednesday, May 28, 2014 4:35 AM