c#中的Soap请求
本文关键字:请求 Soap 中的 | 更新日期: 2023-09-27 18:16:04
我有一个soap请求,我写在http &javascript,但我似乎无法正确地将其转换为c#。
原始(作品):
<button onclick="doStuff()" type="submit">Send</button>
<textarea name="REQUEST_DATA" cols=120 rows=17 >
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<createModifyRequest>
<username>josephs</username>
<lookupIds>
<lookupIds>4225</lookupIds><!--firepass-->
</lookupIds>
</createModifyRequest>
</soap:Body>
</soap:Envelope>
</textarea>
<script language="javascript">
function doStuff() {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
xmlhttp.open("POST", "http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest", false);
xmlhttp.setRequestHeader("SOAPAction", "createModifyRequest");
var userpass = "josephs" + ":" + "pass";
xmlhttp.setRequestHeader("Authorization", "Basic " + (userpass));
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(REQUEST_DATA.value);
}
在c#中转换(不工作)
private void button1_Click(object sender, EventArgs e)
{
string soap =@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<createModifyRequest>
<username>josephs</username>
<lookupIds>
<lookupIds>4225</lookupIds>
<!--firepass-->
</lookupIds>
</createModifyRequest>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest");
req.Headers.Add("SOAPAction", "'"createModifyRequest'"");
var userpass = "josephs" + ":" + "pass";
req.Headers.Add("Authorization", "Basic " + (userpass));
// req.Headers.Add("Content-Type", "text/xml");
req.ContentType = "text/xml;charset='"utf-8'"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
// TODO: Do whatever you need with the response
}
在我运行c#代码的那一刻,我得到一个内部500服务器错误,所以我做错了什么?
我试图重现您的问题。目前我无法创建您的请求,但我已生成您的数据本地请求。我知道的一件事是,如果我删除utf-8周围的双引号("),它会工作得很好。只是通过 utf - 8字符集= 的 charset = ' " utf - 8 ' " "
您为什么不能直接使用Visual Studio内置的SOAP web服务支持呢?
你可以添加一个服务引用或一个web引用(取决于你所针对的框架版本)。
那么你可以直接使用VS为你创建的代理类。
自己编写所有HTTP代码没有任何好处。实际上,这有一个很大的缺点,因为您无法从SOAP服务的WSDL中获得适当的数据类型。