如何设置实时URL以调用Web服务 [英] How to set live URL to call web service

查看:93
本文介绍了如何设置实时URL以调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码工作正常的本地webservice call.but在输入实时URL时不能正常工作。

网址是:

_Default Web服务 [ ^ ]







如何从中调用该Web服务我的项目的实时网址



我的尝试:



< pre lang =c#> private string CallWebService()
{
< span class =code-keyword> 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 ;
尝试
{
myWriter = new StreamWriter(objRequest.GetRequestStream) ());
myWriter.Write(strPost);

}
catch (例外e)
{
throw new 异常(e.ToString());
}
最后
{
myWriter.Close();
}
HttpWebResponse objResponse =(HttpWebResponse)objRequest.GetResponse();
使用(StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close();
}
返回结果;
}

解决方案

你写过

string url =;

if(Request.IsLocal)

{

url =http:// localhost:2111 / UPDB2016 / Default.asmx / AddNominations ;

}

哪里还有其他部分?

当您在Create()方法中传递url以创建ObjectRequest时。

HttpWebRequest objRequest =(HttpWebRequest)WebRequest.Create(url);



当此CallWebService()方法将该时间url设置为空白时。

在其他部分设置托管链接,即托管WebService。

否则

{

url =YourDomainname / Webserive / Default.asmx / AddNominations

}

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:

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;
   }

解决方案

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"
}


这篇关于如何设置实时URL以调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆