新手诚心求教:C#调用这个WebService的解决方法(结构体数组参数)

nocry115 2016-10-11 10:33:52
因为项目要求,需用C#调用对方的WebService做接口;因以前未接触过C#,在此诚心求教:
在win7上安装了VS2010,新建了一个windows窗体应用程序C#项目webservice1;窗口放了1按钮与1文本框
对方的WSDL文件,放在了项目目录里
d:\documents\visual studio 2010\Projects\webservice1\SI_03AA6_SNZYXT2ERP_JHDGBService.wsdl
在【项目方案资源管理器】里,通过【引用】-【添加服务引用】,带入这个wsdl,命名空间为ws1:

在【对象浏览器】里发现这个ws1有2个interface和4个类结构

对方的WebService只定义了1个函数SI_03AA6_SNZYXT2ERP_JHDGB,函数仅有1个形参结构体数组参数(ref ZSSD_JHDGB[ ] ),返回1字符串string

结构体ZSSD_JHDGB有15个string成员


在按钮里写了调用代码,整体程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace webservice1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ws1.ZSSD_JHDGB[] sz1;
sz1 = new ws1.ZSSD_JHDGB[1]; //结构数组参数定义

ws1.ZSSD_JHDGB sj;
sj = new ws1.ZSSD_JHDGB();

sj.VBELN = "3000059932"; //对结构数据赋值
sj.POSNR = "000010";
sj.LFIMG = "1";
sz1[0] = sj; //对结构数组赋值

string uname, upass;
uname = "***";
upass = "***";

System.Net.NetworkCredential c = new System.Net.NetworkCredential(uname, upass);
ws1.SI_03AA6_SNZYXT2ERP_JHDGBClient s = new ws1.SI_03AA6_SNZYXT2ERP_JHDGBClient();
//s.Credentials = c; //
//s.ClientCredentials.UserName.UserName = uname;
//s.ClientCredentials.UserName.Password = upass;

string rs = s.SI_03AA6_SNZYXT2ERP_JHDGB(ref sz1);

//MessageBox.Show(rs);
label1.Text = rs;
//label1.Text = "ok";
}
}
}
//整体代码END
①学习了网上达人,但身份验证时未找到服务的Credentials属性,不知道ClientCredentials行不行?
②不知道是不是服务SI_03AA6_SNZYXT2ERP_JHDGBClient找错了,调试运行就出错,显示有重载……


因为初次用C#,不知道问题出在哪,请各位达人不吝指导!!

...全文
397 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你的确定左边的高级按钮啊,点进去就能看到添加web引用了
夏天的枫 2016-10-11
  • 打赏
  • 举报
回复
引用 9 楼 hjq624779687 的回复:
[quote=引用 7 楼 nocry115 的回复:] [quote=引用 3 楼 hjq624779687 的回复:] [quote=引用 2 楼 hjq624779687 的回复:] 请检查配置文件
app.config[/quote] 感谢!请问下配置文件是不是有问题: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="Transport"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://shpidci.sh217.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTP_Port" /> <endpoint address="https://shpidci.sh217.com:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTPS_Port" /> </client> </system.serviceModel> </configuration>[/quote] 显然有重复的啊 <endpoint address="https://shpidci.sh217.com:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTPS_Port" /> 干掉一个[/quote] 去掉一个应该就可以了
夏天的枫 2016-10-11
  • 打赏
  • 举报
回复
引用 7 楼 nocry115 的回复:
[quote=引用 3 楼 hjq624779687 的回复:] [quote=引用 2 楼 hjq624779687 的回复:] 请检查配置文件
app.config[/quote] 感谢!请问下配置文件是不是有问题: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="Transport"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://shpidci.sh217.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTP_Port" /> <endpoint address="https://shpidci.sh217.com:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTPS_Port" /> </client> </system.serviceModel> </configuration>[/quote] 显然有重复的啊 <endpoint address="https://shpidci.sh217.com:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTPS_Port" /> 干掉一个
  • 打赏
  • 举报
回复
报错信息是说,你们乱改了配置文件,或者是胡乱用多种不同方式添加了服务(从而搅乱了配置)。 删除所有的服务,重新添加服务。不管你用什么方式添加服务,保证按照教程的成熟操作仅仅来操作一次,不要反复乱试验。
nocry115 2016-10-11
  • 打赏
  • 举报
回复
引用 3 楼 hjq624779687 的回复:
[quote=引用 2 楼 hjq624779687 的回复:] 请检查配置文件
app.config[/quote] 感谢!请问下配置文件是不是有问题: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> <binding name="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="Transport"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://shpidci.sh217.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTP_Port" /> <endpoint address="https://shpidci.sh217.com:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_SNZYXT_DEV&receiverParty=&receiverService=&interface=SI_03AA6_SNZYXT2ERP_JHDGB&interfaceNamespace=http%3A%2F%2Fwww.shenhuagroup.com.cn%2FSD%2FSNZYXT" binding="basicHttpBinding" bindingConfiguration="SI_03AA6_SNZYXT2ERP_JHDGBBinding1" contract="ws1.SI_03AA6_SNZYXT2ERP_JHDGB" name="HTTPS_Port" /> </client> </system.serviceModel> </configuration>
nocry115 2016-10-11
  • 打赏
  • 举报
回复
引用 4 楼 Chinajiyong 的回复:
给个详细点的参考给你:http://www.cnblogs.com/anbylau2130/p/3481659.html
感谢指点! 因为用C#调用对方的webservice主要是为了主程序调用,主程序是PB开发的,对结构体数组参数的webservice不支持;所以想用C#做中转。例如把C#调用的模块做成dll,或者用C#再做个中间webservice方法供主程序调用……
nocry115 2016-10-11
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
高级,添加web引用,不要以wcf的方式添加引用
感谢!麻烦能详细说明下吗?没接触过C# 我们的最终目的是用C#调用webservice成功的话就做成dll,以供主程序调用,所以先做个WCF测试……
EnForGrass 2016-10-11
  • 打赏
  • 举报
回复
给个详细点的参考给你:http://www.cnblogs.com/anbylau2130/p/3481659.html
夏天的枫 2016-10-11
  • 打赏
  • 举报
回复
引用 2 楼 hjq624779687 的回复:
请检查配置文件
app.config
夏天的枫 2016-10-11
  • 打赏
  • 举报
回复
请检查配置文件
  • 打赏
  • 举报
回复
高级,添加web引用,不要以wcf的方式添加引用
nocry115 2016-10-11
  • 打赏
  • 举报
回复
引用 11 楼 starfd 的回复:
你的确定左边的高级按钮啊,点进去就能看到添加web引用了
大侠正解!万分感谢!
飞天凤凰601 2016-10-11
  • 打赏
  • 举报
回复
如果不会C#,这么讲起有点麻烦,我当时学webservice 加测试用了一周时间,

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧