Showing posts with label Invoke Web Service using HttpWebRequest. Show all posts
Showing posts with label Invoke Web Service using HttpWebRequest. Show all posts

Wednesday, July 1, 2009

Call WebService from Code Behind

/*
For a web method like this
[WebMethod]
public string GetAfterDates(int noOfDays)
{
return DateTime.Now.AddDays(noOfDays).ToShortDateString();
}
You can write Web Service Call from Code behind like this.
*/
WebProxy proxy = new WebProxy("http://192.168.1.110:8080");
proxy.BypassProxyOnLocal = true;

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("http://localhost/MyDataProviderWS/MyDataProvider.asmx/GetAfterDates");// this nmae of method u want to call
webRequest.Method = "POST";
webRequest.ProtocolVersion = HttpVersion.Version11;
webRequest.ContentType = "application/x-www-form-urlencoded";
string content = "noOfDays=3"; // input parameter if u have more that one //a=b&dd=aa
webRequest.Proxy = proxy;
webRequest.ContentLength = content.Length;
Stream wri = webRequest.GetRequestStream();
byte[] array = Encoding.ASCII.GetBytes(content);
wri.Write(array, 0, array.Length);
wri.Flush();
wri.Close();
WebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
Stream reader = rsp.GetResponseStream();
byte[] respose = new byte[10000];
reader.Read(respose, 0, respose.Length);
Response.Write(Encoding.ASCII.GetString(respose));