Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Code working fine local webservice call.but live not working while enter live URL.
the url is:
_Default Web Service[^]



How to call that webservice from live url to my project

What I have tried:

C#
private string CallWebService()
   {
       string url= "";
      if (Request.IsLocal)
      {
          url = "http://localhost:2111/UPDB2016/Default.asmx/AddNominations";

      }
       string result = "";
       string strPost = "";
       StreamWriter myWriter = null;
       HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
       objRequest.Method = "POST";
       objRequest.Timeout = 200000;
       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;
   }
Posted
Updated 6-Apr-17 6:09am
v2

1 solution

You have written
string url= "";
if (Request.IsLocal)
{
url = "http://localhost:2111/UPDB2016/Default.asmx/AddNominations";
}
where is else part?
As you are passing the url in Create() method to create the ObjectRequest.
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

When this CallWebService() method calls that time url setted as blank.
In else part set your hosted link where you host your WebService.
Else
{
url ="YourDomainname/Webserive/Default.asmx/AddNominations"
}
 
Share this answer
 
Comments
GrpSMK 7-Apr-17 1:32am    
same error..web config file have to write url?
Ramesh Kumar Barik 7-Apr-17 2:36am    
Yes you can.
If you know the webservice (Hosted) URL then you can set this URL link in you web config file and in application you can use this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900