vb.net - 使用.NET库中的Web服务

时间:2016-06-01 12:45:15

标签: vb.net web-services

我试图将vb.net代码迁移到Windev,但我没有使用网络服务。

我尝试使用的网络服务(h ere is the url link to the wsdl file)发送一个压缩的xml文件,响应是一个压缩的xml文件,如果我发送的文件是否被接受,则说明。

在这个Web服务中,我调用sendBill函数,关于参数: - fileName:string value,包含要发送的zip文件名(需要.zip扩展名) - contentFile:字节数组值,表示要发送的zip文件。

返回的值是一个字节数组值,表示收到的zip文件。

我尝试发送this zip file,因此文件名参数值为20450835600-01-FF11-56.zip

Web服务的调用需要标识,这里是id标识: 用户名:20450835600MODDATOS 密码:MODDATOS

所以,关于我最初的问题,我决定通过.NET库使用Web服务,这是我的库的代码:

Public Class ServiceSunat
Private _ZIPFile As String
Private _ZIPFileResponse As String
Private _sunat_ruc As String

Public Property ZIPFile()
    Get
        Return _ZIPFile
    End Get
    Set(value)
        _ZIPFile = value
    End Set
End Property

Public Property ZIPFileResponse()
    Get
        Return _ZIPFileResponse
    End Get
    Set(value)
        _ZIPFileResponse = value
    End Set
End Property

Public Property sunat_ruc()
    Get
        Return _sunat_ruc
    End Get
    Set(value)
        _sunat_ruc = value
    End Set
End Property

Public Function SendZIPFile() As String
    Try
        If _ZIPFile = "" Then
            Throw New Exception("The ZIPFile value is missing")
        End If

        If _ZIPFileResponse = "" Then
            Throw New Exception("The ZIPFileResponse value is missing")
        End If

        If _sunat_ruc = "" Then
            Throw New Exception("The sunat_ruc value is missing")
        End If

        Dim NomFichierZIP As String = System.IO.Path.GetFileName(_ZIPFile)
        Dim usernametoken_login As String = sunat_ruc & "MODDATOS"
        Dim usernametoken_pwd As String = "MODDATOS"

        System.Net.ServicePointManager.UseNagleAlgorithm = True
        System.Net.ServicePointManager.Expect100Continue = False
        System.Net.ServicePointManager.CheckCertificateRevocationList = True

        Dim ws As New ServiceBeta.billServiceClient
        Dim Retour As Byte()

        Dim data As Byte() = System.IO.File.ReadAllBytes(_ZIPFile)

        ws.ClientCredentials.CreateSecurityTokenManager()
        ws.ClientCredentials.UserName.UserName = usernametoken_login
        ws.ClientCredentials.UserName.Password = usernametoken_pwd

        ws.Open()
        Retour = ws.sendBill(NomFichierZIP, data)
        ws.Close()

        System.IO.File.WriteAllBytes(ZIPFileResponse, Retour)

        Return ""
    Catch ex As Exception
        If ex.Message = "" Then
            Return "Unknown error encountered"
        Else
            Return ex.Message
        End If
    End Try

End Function

结束班

关于我的app.config文件:



<?xml version="1.0"?>
<configuration>
  <configSections>
  </configSections>
  <system.diagnostics>
    <sources>
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information"/>
    </switches>
    <sharedListeners>
      <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter"/>
    </sharedListeners>
  </system.diagnostics>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BillServicePortBinding" 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>
        <binding name="secured">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://www.sunat.gob.pe:443/ol-ti-itcpgem-beta/billService"
			binding="basicHttpBinding" bindingConfiguration="BillServicePortBinding"
			contract="ServiceBeta.billService" name="BillServicePort" >
        <headers>
          <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken >
              <wsse:Username>XXXXXXXXXXXXXXXXXXXMODDATOS</wsse:Username>
              <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>MODDATOS</wsse:Password>
            </wsse:UsernameToken>
          </wsse:Security>
        </headers>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>
&#13;
&#13;
&#13;

使用我的库的代码:

Module Module1

Sub Main()
    Dim EnvoiSunat As New SUNATbeta.ServiceSunat
    Dim l_messageRetour As String = ""

    With EnvoiSunat
        .sunat_ruc = "20450835600"
        .ZIPFile = "C:\Mes Projets\EnvoiWindev\20450835600-01-FF11-56.zip"
        .ZIPFileResponse = "C:\Mes Projets\EnvoiWindev\sunatResponse.zip"
        l_messageRetour = .SendZIPFile
    End With

    If l_messageRetour = "" Then
        MsgBox("ok")
    Else
        MsgBox(l_messageRetour, MsgBoxStyle.Critical)
    End If

End Sub

结束模块

但我遇到此错误消息:

  

找不到引用合同&#39; ServiceBeta.billService&#39;的默认端点元素。在ServiceModel客户端配置部分中。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

我不知道此错误消息代表什么以及如何解决此问题......

由于

PS:

  1. 此网络服务仅用于验证下次我尝试通过制作时发送的zip文件是否可以接受

  2. 因为它是一个测试网络服务,每个用户都拥有完全相同的密码,只有用户名的数字部分可以更改

0 个答案:

没有答案