locked
Read and Invoke Web Service RRS feed

  • Question

  • User429415559 posted

    Hi guys,

    I apologize if my question is stupid!

    I try to make simple Web Form in ASP.NET and Invoke or Read some public Web Service. Can you give me some advice or exaple to do this.

    Thank you very much.

    Best regards

    Monday, December 2, 2013 5:53 PM

Answers

  • User-417640953 posted

    I try to make simple Web Form in ASP.NET and Invoke or Read some public Web Service. Can you give me some advice or exaple to do this.

    Hi ivan,

    Thank you post the issue to asp.net forum.

    For calling public web service in our asp.net application. There are two ways for you.

    First of all, making sure you have the url of the public web service.

    # Call webservice in the code behind.

       In the code behind, you choice use "HttpWebRequest" to call the webservice. Some one has provided below code.

    string result =  "";
     
    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.myURL.com/MyWebService.asmx/FunctionName");
     
    myRequest.Method = "POST";
    
    using (StreamWriter requestWriter = new StreamWriter(myRequest.GetRequestStream()))
    
    {
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("zipCode"), HttpUtility.UrlEncode("34275"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("distanceLimit"), HttpUtility.UrlEncode("50"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("maxResults"), HttpUtility.UrlEncode("50"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("includeBigDealers"), HttpUtility.UrlEncode("0"));
    
    }
    
    using (StreamReader responseReader = new StreamReader(myRequest.GetResponse().GetResponseStream()))
    
    {
    
    result = responseReader.ReadToEnd();
    
    }
    

    # Call public webservice using the javascript.

       If you choice the javascript, you should use the "service.useService" to complete it.

     service.useService(http://localhost:1394/MyWebService.asmx?wsdl, 
    	"GetDateTimeService");
     service.GetDateTimeService.callService("GetDateTime");
    

    For more information, please check below link.

    http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr

     

    Thanks.

    Best Regards!

      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 3, 2013 2:27 AM
  • User-484054684 posted

    Another way if you want to call your webservice from your C# file or .aspx.cs file - you can create proxy class for the webservice and call it just like normal methods.

    You may go through these references for the complete step by step process and the screenshots.

    In short - Right click on project - and add your webservice reference - it will generate the proxy class (C#) file for you - It contains the webmethods exposed by webservice.

    You can directly use that C# file to invoke the webmethods exposed by the webservice. We need to supply the url of the webservice and this url can be placed later in the web.config file to configure different urls for different environments.

    http://www.tutorialspoint.com/asp.net/asp.net_web_services.htm

    http://www.aspdotnet-suresh.com/2011/05/aspnet-web-service-or-creating-and.html

    http://msdn.microsoft.com/en-us/library/8wbhsy70(v=vs.90).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 3, 2013 2:37 AM

All replies

  • User386652321 posted

    Hi,

    From .net 3.5 onwards you can code in WCF to create and consume web services. 2.0 and old versions have .asmx for web service.

    You need to learn both how to create WCF Service and how to consume WCF service pls.

     

    Monday, December 2, 2013 10:20 PM
  • User-417640953 posted

    I try to make simple Web Form in ASP.NET and Invoke or Read some public Web Service. Can you give me some advice or exaple to do this.

    Hi ivan,

    Thank you post the issue to asp.net forum.

    For calling public web service in our asp.net application. There are two ways for you.

    First of all, making sure you have the url of the public web service.

    # Call webservice in the code behind.

       In the code behind, you choice use "HttpWebRequest" to call the webservice. Some one has provided below code.

    string result =  "";
     
    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.myURL.com/MyWebService.asmx/FunctionName");
     
    myRequest.Method = "POST";
    
    using (StreamWriter requestWriter = new StreamWriter(myRequest.GetRequestStream()))
    
    {
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("zipCode"), HttpUtility.UrlEncode("34275"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("distanceLimit"), HttpUtility.UrlEncode("50"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("maxResults"), HttpUtility.UrlEncode("50"));
    
    requestWriter.Write("&{0}={1}", HttpUtility.UrlEncode("includeBigDealers"), HttpUtility.UrlEncode("0"));
    
    }
    
    using (StreamReader responseReader = new StreamReader(myRequest.GetResponse().GetResponseStream()))
    
    {
    
    result = responseReader.ReadToEnd();
    
    }
    

    # Call public webservice using the javascript.

       If you choice the javascript, you should use the "service.useService" to complete it.

     service.useService(http://localhost:1394/MyWebService.asmx?wsdl, 
    	"GetDateTimeService");
     service.GetDateTimeService.callService("GetDateTime");
    

    For more information, please check below link.

    http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr

     

    Thanks.

    Best Regards!

      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 3, 2013 2:27 AM
  • User-484054684 posted

    Another way if you want to call your webservice from your C# file or .aspx.cs file - you can create proxy class for the webservice and call it just like normal methods.

    You may go through these references for the complete step by step process and the screenshots.

    In short - Right click on project - and add your webservice reference - it will generate the proxy class (C#) file for you - It contains the webmethods exposed by webservice.

    You can directly use that C# file to invoke the webmethods exposed by the webservice. We need to supply the url of the webservice and this url can be placed later in the web.config file to configure different urls for different environments.

    http://www.tutorialspoint.com/asp.net/asp.net_web_services.htm

    http://www.aspdotnet-suresh.com/2011/05/aspnet-web-service-or-creating-and.html

    http://msdn.microsoft.com/en-us/library/8wbhsy70(v=vs.90).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 3, 2013 2:37 AM