Web service client : WebService « Network « C# / CSharp Tutorial






using System;
using System.IO;
using System.Net;
using System.Xml.XPath;

public class MainClass{

  public static void Main(string [] args) {
    string content = "productCode=1111";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:80/Query.asmx/GetNumberInStock");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = content.Length;

    StreamWriter streamWriter = new StreamWriter(request.GetRequestStream( ));
    streamWriter.Write(content);
    streamWriter.Flush( );

    WebResponse response = request.GetResponse( );
    Stream stream = response.GetResponseStream( );

    XPathDocument document = new XPathDocument(stream);
    XPathNavigator nav = document.CreateNavigator( );
    XPathNodeIterator nodes = nav.Select("//int");
    Console.WriteLine(nodes.Current);
  }
}








33.21.WebService
33.21.1.Simple WebService
33.21.2.Simplest web method
33.21.3.Web service client