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 =”http://abc.com”;

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

streamWriter.Write(json);
}

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.

<system.webServer>

<httpProtocol>

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

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

</customHeaders>

</httpProtocol>

</system.webServer>

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

Add below code in web.config file.

<system.web>

<webServices>

<protocols>

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

</protocols>

</webServices>

</system.web>