简体   繁体   中英

How to get json string as response using Webhook in asp.net c# Webforms

My SMS company have some Webhook method to send the delivery report to our preferred url, then we have to get the data from that url and update it as log.

For this, I am using below code:

HttpWebRequest request = WebRequest.Create("http://example.com/sms/response.html") as HttpWebRequest;
        request.UserAgent = "jaidevkh";
        request.ContentType = "text/html,application/xhtml+xml,application/xml; charset=utf-8";
        request.Accept = "text/html,application/xhtml+xml,application/xml";
        request.Method = "GET";
        //request.ContentLength = 0;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        WebHeaderCollection header = response.Headers;
        var encoding = ASCIIEncoding.ASCII;
        string responseText = "";
        using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
        {
            responseText = reader.ReadToEnd();
        }
        Label kl = (Label)cph.FindControl("j");
        kl.Text = responseText.ToString();

and the json format to come as per SMS vendor in as below:

data=[
      {
          "requestId":"35666a716868323535323239",
          "userId":"38229",
          "report":[  
              {  
                  "desc":"REJECTED",
                  "status":"16",
                  "number":"91XXXXXXXXXX",
                  "date":"2015-06-10 17:09:32.0"
              }
          ],
          "senderId":"tester"
      },
      {  
          "requestId":"35666a716868323535323239",
          "userId":"38229",
          "report":[  
              {  
                  "desc":"REJECTED",
                  "status":"16",
                  "number":"91XXXXXXXXXX",
                  "date":"2015-06-10 17:09:32.0"
              },
              {  
                  "desc":"REJECTED",
                  "status":"16",
                  "number":"91XXXXXXXXXX",
                  "date":"2015-06-10 17:09:32.0"
              }
          ],
          "senderId":"tester"
      }
  ]

here, this gives responseText as blank. Please suggest what is wrong with this code.

Thanks

Jay

This is an example for receiveing data from your vendor.

firstly you have to create two models for getting data by vendor format :

public class Response
    {
        public string RequestId { get; set; }
        public string UserId { get; set; }
        public List<ResponseReport> Report { get; set; }
        public string SenderId { get; set; }
    }

    public class ResponseReport
    {
        public string Desc { get; set; }
        public int Status { get; set; }
        public string Number { get; set; }
        public DateTime Date { get; set; }

    }

those model is created by your json format

after that you have to create a handler for getting data (preferred url).

public class GetDelivery : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {


            var jsonSerializer = new JavaScriptSerializer();

            var jsonString = string.Empty;

            context.Request.InputStream.Position = 0;
            using (var inputStream = new StreamReader(context.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }

            var data = new List<Response>();
            data = jsonSerializer.Deserialize<List<Response>>(jsonString);


            //Modification and Saving Data

            context.Response.Write("OK");

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

in this handler you can get what the vendor sent to you, and you can modify and save it.

Note: the data model and json format must be similar to each other

Testing Code

Finally for testing send data by vendor you can use this code :

<button id="send" class="CallHandler">Send</button>
    <script>

        $(".CallHandler").click(function () {
            alert('test');
            $.ajax({
                url: "GetDelivery.ashx",
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                dataType: "json",
                data: JSON.stringify([
                    { "requestId": "35666a716868323535323239", "userId": "38229", "report": [{ "desc": "REJECTED", "status": "16", "number": "91XXXXXXXXXX", "date": "2015-06-10 17:09:32.0" }], "senderId": "tester" },
                    { "requestId": "35666a716868323535323239", "userId": "38229", "report": [{ "desc": "REJECTED", "status": "16", "number": "91XXXXXXXXXX", "date": "2015-06-10 17:09:32.0" }, { "desc": "REJECTED", "status": "16", "number": "91XXXXXXXXXX", "date": "2015-06-10 17:09:32.0" }], "senderId": "tester" }
                ]),

            });
            return false;
        });


    </script>

I hope it satisfy you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM