asp.net page cannot invoke SOAP POST method in middle-tier
I have a ASP.NET (C#) web page which utilizes a VB class library. The VB library performs a SOAP POST to a remote web service and returns a message. However the VB library keeps running into a "No connection could be made because the target machine actively refused it xxx.xxx.xxx.xxx"
However, I've also created a C# test client which consumes the same VB class library and can perform the post just fine!
So what is going on here?
Default.aspx:
protected void Page_Load(object sender, EventArgs e)
{
String soapString = ........
MyVBMiddleTierObject.PostMgr pMgr = new MyVBMiddleTierObject.PostMgr();
Response.Write(pMgr.DoPOST(soapString));
}
VB Middle Tier:
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# Test client:
class Program
{
static void Main(string[] args)
{
String soapString = ........
MyVBMiddleTierObject.PostMgr pMgr = new MyVBMiddleTierObject.PostMgr();
String s = pMgr.DoPOST(soapString);
Console.WriteLine(s);
}
}
So why is my C# Test Client working just fine, but my web page not?
Update
Soap Message:
<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>
Again, the exact same soap string is being posted (by my test client, and by my web page) but only my test client can connect...
Update to include web.config
<configuration>
<appSettings>
<add key="SoapService" value="https://www.wxy.com/theirService"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true" str开发者_开发百科ict="false" explicit="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
It's pretty basic...
The error means the computer your code connected to does not have a server listening on the port you requested (80 I assume). That could mean that the IP is wrong (it found a computer, but the wrong one), or the port is wrong. We would need to see your soapString, to further diagnose.
try putting this in your web.config to ensure credentials are passed along to your web service.
<identity impersonate="true" />
or if you are not using a windows token, just specify the userid and password.
<identity impersonate="true"
userName="domain\user"
password="password" />
You also need to use a proxy with credentials.
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
or
client.Proxy.Credentials = New Net.NetworkCredential("UserName", "Password")
精彩评论