asp.net页面无法在中间层调用SOAP POST方法

时间:2010-07-27 17:15:44

标签: c# asp.net vb.net web-services

我有一个使用VB类库的ASP.NET(C#)网页。 VB库对远程Web服务执行SOAP POST并返回消息。然而,VB库一直运行“无法建立连接,因为目标机器主动拒绝它xxx.xxx.xxx.xxx”

但是,我还创建了一个C#测试客户端,它使用相同的VB类库并且可以很好地执行帖子!

那么这里发生了什么?

Default.aspx的:

protected void Page_Load(object sender, EventArgs e)
    {
        String soapString = ........
        MyVBMiddleTierObject.PostMgr pMgr = new MyVBMiddleTierObject.PostMgr();
        Response.Write(pMgr.DoPOST(soapString));
    }

VB中间层:

Public Function DoPOST(ByVal soapMsg As String) As String
  Dim encode As UTF8Encoding = New UTF8Encoding()
  Dim buff() As Byte = encode.GetBytes(soapMsg)

  Dim client As HttpWebRequest = CType(WebRequest.Create(ConfigurationManager.AppSettings("SoapService")), HttpWebRequest)
  client.Headers.Add("SOAPAction", "")
  client.ContentType = "text/xml;charset=utf-8"
  client.Accept = "text/xml"
  client.Method = "POST"
  client.ContentLength = buff.Length
  client.Timeout = 5000

  Dim s As Stream = client.GetRequestStream()
  s.Write(buff, 0, buff.Length)
  s.Close()

  Dim response As WebResponse = client.GetResponse()
  s = response.GetResponseStream()
  Dim sr As StreamReader = New StreamReader(s)
  sPostResult = sr.ReadToEnd()
  response.Close()

  DoPOST = sPostResult

End Function

C#测试客户端:

class Program
    {
        static void Main(string[] args)
        {
            String soapString = ........
            MyVBMiddleTierObject.PostMgr pMgr = new MyVBMiddleTierObject.PostMgr();
            String s = pMgr.DoPOST(soapString);
            Console.WriteLine(s);
        }
    }

那么为什么我的C#测试客户端工作得很好,但我的网页没有?

更新

肥皂消息:

<soapenv:Envelope xmlns:java="java:com.xyz.service"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:vcs="http://schemas.xyz.com/vcs">
  <soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-5775010" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility- 1.0.xsd">
        <wsse:Username>JoeShmoe</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">123456789</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
  <soapenv:Body>
    <vcs:theirService>
      <vcs:theirMethod>
        <java:Frequency>M</java:Frequency>
      </vcs:theirMethod>
    </vcs:theirService>
  </soapenv:Body>
</soapenv:Envelope>

同样,我发布了完全相同的肥皂串(由我的测试客户端和我的网页发布),但只有我的测试客户端可以连接......


更新以包含web.config

<configuration>
    <appSettings>
        <add key="SoapService" value="https://www.wxy.com/theirService"/>
    </appSettings>
    <connectionStrings/>
    <system.web>
        <compilation debug="true" strict="false" explicit="true"/>
        <authentication mode="Windows"/>
    </system.web>
</configuration>

这很基本......

1 个答案:

答案 0 :(得分:2)

错误意味着您的代码所连接的计算机没有服务器侦听您请求的端口(我假设为80)。这可能意味着IP是错误的(它发现了计算机,但错误的是),或者端口是错误的。我们需要看到你的soapString,以进一步诊断。

尝试将其放入您的web.config中,以确保将凭据传递到您的Web服务。

<identity impersonate="true" />

或者如果您没有使用Windows令牌,只需指定用户ID和密码。

<identity impersonate="true"
          userName="domain\user" 
          password="password" />

您还需要使用带凭据的代理。

client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials

client.Proxy.Credentials = New Net.NetworkCredential("UserName", "Password")