.net 调用外部的webservice接口

需求:做这个项目之前需要用到一个java的webservice接口,首先想到的第一种方法就是添加服务引用了.但是这样的话会有一个config文件 生成的dll必须要读取config文件,config文件放到同一目录也不行的,需要放到另一个工具的节点中才能被另外一个工具读取,不幸的是另外一个工具不能添加节点 否者会boom

二:接下来就是只能手写地址写进去程序里面.然后在百度搜索了一下文章.代码不出意外的粘贴复制,然后自己写个c#的webserive测试完全没问题..发现给java用时候就不行了.百思不得其解,报错找不到命名空间之类的,那就不折腾了.

三:wcf提供的吧. 首先需要一个工厂方法和一个代理类.代理类可以用wcf工具生成,具体步骤如下http://blog.163.com/[email protected]/blog/static/3743053820132133616282/引用别人写的吧  照着做就行这是原地址.这个时候就拿到了一个代理类,然后找一个工厂的方法

public static T CreateServiceByUrl<T>(string url)
{
return CreateServiceByUrl<T>(url, "basicHttpBinding");
}

public static T CreateServiceByUrl<T>(string url, string bing)
{
try
{
if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!");
EndpointAddress address = new EndpointAddress(url);
Binding binding = CreateBinding(bing);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
return factory.CreateChannel();
}
catch (Exception ex)
{
throw new Exception("创建服务工厂出现异常.");
}
}
#endregion

#region 创建传输协议
/// <summary>
/// 创建传输协议
/// </summary>
/// <param name="binding">传输协议名称</param>
/// <returns></returns>
private static Binding CreateBinding(string binding)
{
Binding bindinginstance = null;
if (binding.ToLower() == "basichttpbinding")
{
BasicHttpBinding ws = new BasicHttpBinding();
ws.MaxBufferSize = 2147483647;
ws.MaxBufferPoolSize = 2147483647;
ws.MaxReceivedMessageSize = 2147483647;
ws.CloseTimeout = new TimeSpan(0, 30, 0);
ws.OpenTimeout = new TimeSpan(0, 30, 0);
ws.ReceiveTimeout = new TimeSpan(0, 30, 0);
ws.SendTimeout = new TimeSpan(0, 30, 0);

bindinginstance = ws;
}
else if (binding.ToLower() == "nettcpbinding")
{
NetTcpBinding ws = new NetTcpBinding();
ws.MaxReceivedMessageSize = 65535000;
ws.Security.Mode = SecurityMode.None;
bindinginstance = ws;
}
else if (binding.ToLower() == "wshttpbinding")
{
WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
ws.MaxReceivedMessageSize = 65535000;
ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
bindinginstance = ws;
}
return bindinginstance;

}

然后下面就是使用的代码了

string url = "这里填写你的接口地址";

客户端生成的接口名 例如public interface AA就这样写

AA a=WcfInvokeFactory.CreateServiceByUrl<AA>(url);

a.方法(new 代理类里面需要调用传参的方法("参数"));

时间: 2024-08-08 22:09:42

.net 调用外部的webservice接口的相关文章

java调用.net的webservice接口

要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client-> 选择JAX-WS方式,点击“next”,看到以下界面:输入webservice接口地址,然后选择你要生成客户端的package包,不选择默认是项目的default包. 点击“next”,进入验证环节,再次点击“next”,进入以下界面:如果在新建项目时new的是web service project,这两项不需要勾选,否则要勾选上.

python实现建立soap通信(调用及测试webservice接口)

实现代码如下: #调用及测试webservice接口 import requests class SoapConnect: def get_soap(self,url,data): r = requests.post(url,data) print(r.text) if __name__ == '__main__': u = 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo' d = {'mobile

.netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析

众所周知,微服务架构是由一众微服务组成,项目中调用其他微服务接口更是常见的操作.为了便于调用外部接口,我们的常用思路一般都是封装一个外部接口的客户端,使用时候直接调用相应的方法.webservice或WCF的做法就是引用服务,自动生成客户端.在webapi2.0里,我们都会手动封装一个静态类.那么在.netcore3.1的微服务时代,我们该如何处理这个问题呢? ----思路都是一样的,封装一个外部服务,并且使用依赖注入和 HttpFactory工厂等.netcore特有的方式提升性能.接下来我们

php中创建和调用webservice接口示例

这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservice客户端例子,需要的朋友可以参考下 作为开发者来讲,要想写webservice接口或者调用别人的webservice接口,首先需要了解什么是webservice.简单说, WebService就是一些站点开放一些服务出来, 也可以是你自己开发的Service, 也就是一些方法, 通过URL,指定某一个方法名,发出请求,站点里的这个服务(方法),接到

php调用webservice接口

项目中使用到了调用三方厂商webService接口.他的接口类似为http://haha.cn:86/BaseInfoService.svc?wsdl,在这里我注意到了"wsdl"以前从来没有过这种接口的经验,起初想用CURL调用,但是一直失败,后来想到了php扩展soap 以下为我的代码: function SendLeads($data){ header('Content-Type:text/html; charset=utf-8');//设置编码方式UTF-8 ini_set('

android开发调用自定义的webservice

在上一篇,我们开发了自定义的service:使用MyEclipse开发webservice,今天我们用android程序调用自定义的webservice接口: 在浏览器输入http://192.168.0.5:8080/TestService/MyServicePort?wsdl 其中MyServicePort是webservice服务器端的sun-jaxws.xml文件中的url-pattern="/MyServicePort" 注意这里的192.168.0.5是我本机的ip地址,需

使用cxf开发webservice接口

项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互.        比如,平台平台淘宝.京东想获取其他快递公司数据接口,需快递公司开放数据接口.       那么 webservice就是出于以上类似需求而定义出来的规范:无需关心对方什么平台上开发以及使用何种语言开发.       只关心调用

通过iTop Webservice接口丰富OQL的功能

通过Python调用iTop的Webservice接口: #!/usr/bin/env python #coding: utf-8 import requests import json itopurl = "https://itsm.google.cn/webservices/rest.php" # opr = json.dumps( # {'operation':'list_operations'} # ) opr = json.dumps( { 'operation':'core

ajax跨域请求调用webservice接口

1.WebService 接口编写 步骤:新建web项目=>添加web service=>编写方法接口=>然后发布(本地测试可以直接把这个web service运行起来). 关键如何让外部Ajax 调用. 首先,配置WebService 项目配置文件(web.config)红色部分必须配置,这样第三方才能调用接口方法(经测试通过,直接粘贴就ok),不懂可以百度. <configuration> <system.web> <webServices> &l