c# - C# 中的 SOAP 请求

标签 c# javascript xml visual-studio soap

我有一个用 http 和 javascript 编写的 soap 请求,但我似乎无法将其正确转换为 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 周围的双引号 (") 并且它工作正常。 只需传递 charset=utf-8 而不是 charset=\"utf-8\""

我不确定它是否适合你。

关于c# - C# 中的 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16846572/

相关文章:

c# - Azure 解决方案架构 - 网站还是云服务?

c# - ScrollViewer 不使用 UWP Windows 10 Phone 滚动

javascript - 使用自己域的虚拟主机 (Weebly)

javascript - 使用 Node.js 删除 txt 文件中的一行

javascript - 编辑 : Selecting Multiple Classes then toggle classes for each of them

java - Android 在 Activity 中保留 Nav Bar

json - 当我期望 JSON 时得到 "XML Parsing Error: not well-formed"

c# - C# 中的多个 HTTP 请求

c# - C# 中的 void 与 private void

php - config.xml 覆盖导致购物车不可见?