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;
}