[WinForm].NETのWinForm呼び出しWebサービス


 .NET WinForm   Web Service        ASP.NET   Web Service   。 
            ,           “  Web  ”  。 
      ,           Web References   ,      。 
              。 
ProductService.LTPService service = new ProductService.LTPService(); 
string price=service.GetProductPrice("001"); 
MessageBox.Show(price); 
         ,               。 
    HTTP    Web Service 
     Web Service    .NET ,            ?                  Web Service   。 
   :Get      
private void button1_Click(object sender, EventArgs e) 
{ 
    string strURL = "http://localhost:12074/Service1.asmx/ 
             GetProductPrice?ProductId="; 
    strURL += this.textBox1.Text;            
    //    HTTP   
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); 
    //request.Method="get"; 
   HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); 
   Stream s = response.GetResponseStream(); 


//   XML,       

     XmlTextReader Reader = new XmlTextReader(s); 
    Reader.MoveToContent(); 
    string strValue = Reader.ReadInnerXml(); 
    strValue = strValue.Replace("<", "<"); 
    strValue = strValue.Replace(">", ">"); 

    MessageBox.Show(strValue); 
    Reader.Close(); 
} 


   :Post      
private void button2_Click(object sender, EventArgs e) 
{ 
    string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice"; 
    //    HTTP   
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); 
    //Post     
    request.Method = "POST"; 
    //     
    request.ContentType = "application/x-www-form-urlencoded"; 
    //    ,   URL   
    string paraUrlCoded = HttpUtility.UrlEncode("ProductId"); 
    paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text); 
    byte[] payload; 
    // URL             
    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 
    //     ContentLength 
    request.ContentLength = payload.Length; 
    //    ,      
    Stream writer = request.GetRequestStream(); 
    //         
    writer.Write(payload, 0, payload.Length); 
    //      
    writer.Close();            
    //      
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
   Stream s = response.GetResponseStream(); 
    //   XML,       
    XmlTextReader Reader = new XmlTextReader(s); 
    Reader.MoveToContent(); 
    string strValue = Reader.ReadInnerXml(); 
    strValue = strValue.Replace("<", "<"); 
    strValue = strValue.Replace(">", ">"); 
    MessageBox.Show(strValue); 
    Reader.Close(); 
} 


Get   Post         Post      URL            , Get    URL           URL  。 
URL           ,                (       “%20”)。 
  Web Service       XML  ,  ,         WebRequest   ,      XML      ,         。  ,                    WebService  ,    。 

//========================================================

  : web   ,         ,           ,            WebReference 
      WebReference.  webservice    webservice=new WebReference.  webservice  (); 
      ,      web  ,       (   a) 
   webservice.  webservice    w=new a.  webservice  ();   webservice   SMSWEB,    
ERPWebService.SMSWEB WebService = new ERPWebService.SMSWEB(); 
WebService.RecvSMS(Username,Password,F_ID); 
                 ,  WebService          ,       XML      。