Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Saturday, 28 May 2016

calling web service without adding web reference


Introduction:

Here I will explain how to calling web service without adding web reference using C# code using. Sometimes required to call particular web service without adding web reference at that time we need to call that web service using WebRequest. Once we created and deployed web service that time client required to call webservice so how to do this.

So, Now I explaining how to call web service without adding web reference.


Description: 

In previously post I explained to  
file type validation in jquery
Change the column name or datatype or size in sql server
How to set or get textbox value using Javascript or Jquery
Server does not support secure connections  
Three tier architecture in asp.net
Remove .aspx from url in asp.net

And now here I explain to how to call web service without adding web reference using C# code.

Now copy the following code in your page.

this is the method in MyWebService.asmx wants to call without adding web reference
 


    [WebMethod]
    public string MyDemoWebService(string data1, string data2)
    {
       // Your code here

       return "";

    }


 Now copy the following code in code behind where you want to call web service.

private string CallWebService(string data1, string data2)
    {
        if (Request.IsLocal)
        {

        }
        string result = "";
        string strPost = "data1=" + data1+ "&data2=" + data2;
        StreamWriter myWriter = null;
        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
        objRequest.Method = "POST";
        objRequest.Timeout = 200000; // this should be greater than IIS / web server's timeout
        objRequest.ContentLength = strPost.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";
        objRequest.KeepAlive = false;
        try
        {
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(strPost);

        }
        catch (Exception e)
        {
            throw new Exception(e.ToString());
        }
        finally
        {
            myWriter.Close();
        }
        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream)))
        {
            result = sr.ReadToEnd();
            sr.Close();
        }
        return result;
    }
 


  Now call above function CallWebService in your button click event with given parameter and your web service is called without adding web reference. you will get response in result string variavle.

Now run your project and check.

 

 

Friday, 14 February 2014

web service example in asp.net

web service example in asp.net
OR
Addition,subtraction, multiplication and division operation
using web service


if you want to create your own web service than do following

--> first add one web service from right click on solution explorer(current project)
--> set name as ws_calc
--> next following code paste in your web service
------------------------------------------------------------------------------------------------


//Default method
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public int add(int a, int b)
    {
        return a + b;
    }

    [WebMethod]
    public int sub(int a, int b)
    {
        return a - b;
    }

    [WebMethod]
    public int mul(int a, int b)
    {
        return a * b;
    }

    [WebMethod]
    public int div(int a, int b)
    {
        return a / b;
    }

--> copy the following code in your .aspx page
------------------------------------------------------------------------------------------------


<div>
<table class="style1">
<tr>
<td colspan="2">
    <asp:label id="Label11" runat="server" font-names="Arial" font-size="X-Large" forecolor="#0033CC"
        text="Web service Example&nbsp;"></asp:label>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label1" runat="server" text="Enter value"></asp:label>
</td>
<td>
    <asp:textbox id="TextBox1" runat="server">
    </asp:textbox>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label2" runat="server" text="Enter value"></asp:label>
</td>
<td>
    <asp:textbox id="TextBox2" runat="server">
    </asp:textbox>
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:button id="Button1" runat="server" text="Submit" onclick="Button1_Click" />
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label7" runat="server" text="Addition" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label3" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label8" runat="server" text="Substraction" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label4" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label9" runat="server" text="Multiplication" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label5" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    <asp:label id="Label10" runat="server" text="division" font-names="Arial" forecolor="#003300">
    </asp:label>
</td>
<td>
    <asp:label id="Label6" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td class="style2" align="right">
    &nbsp;
</td>
<td>
    &nbsp;
</td>
</tr>
</table>
</div>

--> next following code paste in your code behind file(.aspx.cs file)
------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default: System.Web.UI.Page
{
    WS_calc ws1 = new WS_calc();// This line for create webservice object
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    if (TextBox1.Text == "" || TextBox2.Text == "")
    {
    }
    else
    {
        int a = Convert.ToInt32(TextBox1.Text);
        int b = Convert.ToInt32(TextBox2.Text);

        Label3.Text = ws1.add(a, b).ToString(); //calling web service for Addition
        Label4.Text = ws1.sub(a, b).ToString(); //calling web service for Substraction
        Label5.Text = ws1.mul(a, b).ToString(); //calling web service for Multiplication
        Label6.Text = ws1.div(a, b).ToString(); //calling web service for Division
    }
    }
}


Asp.net tutorials