1

I have send a Json to a web method. While web method not receiving the json as a string.

POST Method Test1.aspx -

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:49633/Test3.aspx/Get");

                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"d\":{\"accessKey\":\"Chennai\",\"channelId\":\"1025\"}}";

                    streamWriter.Write(json);
                }
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var responseText = streamReader.ReadToEnd();
                    return responseText;
                }

WEB Method in Test3.aspx

   [System.Web.Services.WebMethod]
        public static string Get(string d)
        {
            return d;
        }

RESPONCE -

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>

    </title></head>
    <body>
        <form name="form1" method="post" action="Booking.aspx" id="form1">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTg3NjI4NzkzNmRkOvsswBe8G74mfKP2QBvs0WW2jms=" />

        <div>   
  <span id="Label3"><font color="Fuchsia">Responce:-</font></span>
            <span id="Label1">Label</span> 
        </div>
        </form>
    </body>
    </html>

QUERY -

I receive a response as HTML Page. But I want to Receive the Json Which I was Send via POST method

JSON STRING -

{"d":{"accessKey":"Chennai","channelId":"1025"}}   

EDIT

I have changed the json string as {"accessKey":"Chennai","channelId":"1025"} and changed the webmethod to get the string.

 [System.Web.Services.WebMethod]
            public static string Get(string accessKey, string channelId)
            {
                return accessKey + channelId;
            }  

I have received a correct channelId, accesskey values. But my original string is very big. so I need to receive same Json String which i send via POST method. bzs I am doing receiving part only. Once of my Client Send the Json String via Calling this Websmethod. Thanks.

Edit 2-

As per Mez Said I have include the ScriptMethod. But It through The remote server returned an error: (500) Internal Server Error.

 [System.Web.Services.WebMethod]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public static string Get(string d)
            {          
                return d;
            }
Sagotharan
  • 2,586
  • 16
  • 73
  • 117
  • Consider using HTTP Generic Handlers. View link : http://stackoverflow.com/questions/3702959/asp-net-generic-http-handler-ashx-supporting-jsonp. It will avoid sending back content which you do not need, like what is happening in your case... – Mez Jul 14 '14 at 09:16
  • When I change the string as {"accessKey":"Chennai","channelId":"1025"} and webmethod as public static string Get(string accessKey, string channelId) I can able to get the values. But I want a Json Full String,. – Sagotharan Jul 14 '14 at 09:56
  • You are also missing the ScriptMethod response format... – Mez Jul 14 '14 at 10:01
  • see my updated Question Sir. I have Include the ScriptMethod But I got an Error. – Sagotharan Jul 14 '14 at 10:16

1 Answers1

0

It is possible you reached the max limit for json. Try adding this to your web.config.

<configuration>
    ... 
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="300000" />
            </webServices>
        </scripting>
    </system.web.extensions> 
</configuration> 
Mez
  • 4,666
  • 4
  • 29
  • 57
  • I have tried sir. But The remote server returned an error: (500) Internal Server Error through. see my Edit in Question sir, I need to receive the Full Json String. @Mez – Sagotharan Jul 14 '14 at 10:14
  • its not a length problem Mez. Im not receiving the Json. {"d":{"accessKey":"Chennai","channelId":"1025"}} Please Try my codings and give the solutions. – Sagotharan Jul 14 '14 at 10:28