Skip to main content

Call web service without using web reference. Asp.Net

Introduction

Most application have their own webservice act as a API call for other services. In .NET we can use Web Reference in development environment and .net will automatically generate webservice method in development environment. BUT ins some scenario, you cannot add Web Reference in your solution project. Hence this post is a solution for your problem. 

Code Behind

This code behind can call from method, or button event, or can create class. 

       string result = "";  
       StreamReader resStream = null;  
       HttpWebResponse response = null;        
       try  
       {  
         //set webservice address  
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<web service address>");  
         //set webservice method name  
         request.Headers.Add("SOAPAction", "http://tempuri.org/" + "<web service method>");  
         request.ContentType = "text/xml;charset=\"utf-8\"";  
         request.KeepAlive = false;  
         request.Timeout = 300000; // - in millisecond. (5 minit)    
         request.Method = "POST";  
         request.Credentials = CredentialCache.DefaultCredentials;  
         byte[] byteArray = Encoding.ASCII.GetBytes("<data you want to send to webservice>");  
         request.ContentLength = byteArray.Length;  
         Stream s = request.GetRequestStream();  
         s.Write(byteArray, 0, byteArray.Length);  
         s.Dispose();  
         s.Close();  
         response = (HttpWebResponse)request.GetResponse();  
         Encoding enc = System.Text.Encoding.GetEncoding(1252);  
         resStream = new StreamReader(response.GetResponseStream());  
         result = resStream.ReadToEnd(); // read webservice response                
       }  
       catch (Exception ex)  
       {  
         result = ex.Message;  
       }  
       finally  
       {  
         resStream.Dispose();  
         resStream.Close();  
         response.Close();  
       }  

Comments

Popular posts from this blog

Using Smart Search Filter via API/Code behind in Kentico

Introduction This is sample how to use kentico smart search API + Smart Search Filtering via API / Code behind. The goal of this post is to show how you can use Smart Search API stated in this documentation.  https://docs.kentico.com/api9/configuration/smart-search#Smartsearch-Usinganindextosearchthroughtext https://docs.kentico.com/api10/configuration/smart-search#Smartsearch-Usinganindextosearchthroughtext Example code. Please understand on how smart search syntax work before you proceed. You can read about this in kentico documentation below :  https://docs.kentico.com/k9/configuring-kentico/setting-up-search-on-your-website/smart-search-syntax /// <summary> /// Smart Search + Filtering. Change return type to suite your requirement /// </summary> /// <param name="filterValue"> /// Filtering Syntax /// eg : +(Collections:(719c06e0-ccd2-436d-84f7-ef879849ca7e)) +(weddings:(4)) /// Collections and w...

Encrypt Decrypt Web.config files - ASP.NET

Introduction Objective of this tutorial is to encrypt or decrypt web.config files in asp.net development project. Some of the web.config file contains sensitive information such as connection string to database, AppSettings values, etc. The function write in this tutorial is not auto execute, but require human action like click on button. This example will encrypt connectionStrings,appSettings and system.web/authentication section in web.config Example: On asp.net project solution, create one dummy page to and insert 2 buttons in aspx page.  Double click on the button to create an event for each button.  Copy code behind below.  Example Code Front End - ASPX page <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Encrypt" /> <asp:Button ID="Button2" runat="server" On...