C# asp.net页无法调用中间层中的SOAP POST方法
我有一个利用VB类库的ASP.NET(C#)网页。VB库向远程web服务执行SOAP POST并返回消息。但是,VB库不断遇到“无法建立连接,因为目标计算机主动拒绝了它xxx.xxx.xxx.xxx” 不过,我还创建了一个C#测试客户端,它使用相同的VB类库,可以很好地执行post 这是怎么回事 Default.aspx:C# asp.net页无法调用中间层中的SOAP POST方法,c#,asp.net,vb.net,web-services,C#,Asp.net,Vb.net,Web Services,我有一个利用VB类库的ASP.NET(C#)网页。VB库向远程web服务执行SOAP POST并返回消息。但是,VB库不断遇到“无法建立连接,因为目标计算机主动拒绝了它xxx.xxx.xxx.xxx” 不过,我还创建了一个C#测试客户端,它使用相同的VB类库,可以很好地执行post 这是怎么回事 Default.aspx: protected void Page_Load(object sender, EventArgs e) { String soapString =
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#Test客户端工作得很好,而我的网页工作得不好呢
更新
Soap消息:
<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>
乔斯莫
123456789
M
同样,相同的soap字符串正在发布(由我的测试客户端和我的网页发布),但只有我的测试客户端可以连接
更新以包括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>
这是非常基本的…错误意味着您的代码连接到的计算机没有服务器监听您请求的端口(我假设是80)。这可能意味着IP错误(它找到了一台计算机,但却是错误的),或者端口错误。我们需要查看您的soapString,以便进一步诊断 尝试将其放入web.config中,以确保将凭据传递给web服务
<identity impersonate="true" />
或
我不知道,但你为什么要用这种方法呢。您只需添加一个服务引用并使用代理类就可以了。这似乎是十亿次的默认答案。我不是因为我正在向他们的web服务执行SOAP POST,而是因为我发送了相同的SOAP字符串。C#客户端和ASP.NET网页发送相同的soap字符串。但是C#客户端实际上可以发布它,而ASP.NET客户端会收到错误。问题可能与身份验证有关,因为ASP.NET页面必须模拟客户端才能向web服务发出第二个请求。你能发布你的web.config身份验证信息吗?我也想到了这个想法,但是谁发布、登录用户还是ASPNET帐户都不重要。试着在你的web.config中添加“”。这将消除安全问题。这解决了它,谢谢!当然,我的测试客户端是在我的凭据下运行的,但是该网页是以NetworkService或ASPNET的形式运行的,并且在代理服务器上被阻止。
<identity impersonate="true"
userName="domain\user"
password="password" />
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
client.Proxy.Credentials = New Net.NetworkCredential("UserName", "Password")