Archive for the ‘Web Service’ Category

Send Json Data to Webservice and get response from Webservice

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Data;
using System.Web.Script.Serialization;


string url =””;

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = “application/json”;
httpWebRequest.Method = “POST”;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
string json = new JavaScriptSerializer().Serialize(new
empid = “1001”,
empname = “test name”,
empsalary= “10000”


var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
var result = streamReader.ReadToEnd();


Enable Cross-domain Ajax requests using web.config

Error : 

XMLHttpRequest cannot load webservice.asmx. No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Solution : 

Add Below code in web.config file.



<!–Allow Web API to be called from a different domain.–>

<add name=”Access-Control-Allow-Origin” value=”*” />




Enable ASP.NET ASMX web service for HTTP POST / GET requests

Add below code in web.config file.




<add name=”HttpGet” />
<add name=”HttpPost” />