如何通过ASP.NET设置SOAP请求到URL作为服务?

时间:2014-02-04 17:12:22

标签: asp.net soap httpwebrequest

在我的Default.aspx文件中包含以下代码,位于http://localhost/idss/Default.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head id="Head1" runat="server">
    <title>Testing IDSS Return Values</title>
</head>
<body>
<%@

// how we do it
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("idss/requests/categoryList.xml"));

// create the request to your URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/idss/Default.aspx");

// add the headers
// the SOAPACtion determines what action the web service should use
// YOU MUST KNOW THIS and SET IT HERE
request.Headers.Add("SOAPAction", "http://ws.idssasp.com/Members.asmx/GetCategoryList");

// set the request type
// we use utf-8 but set the content type here
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";

// add our body to the request
Stream stream = request.GetRequestStream();
doc.Save( stream );
stream.Close();

// get the response back
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
     request.Write(response);
}//end using


 %>
</body>

</html>

位于:http://localhost/idss/requests/categoryList.xml的xml文件如下所示:

<?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:Header>
    <AuthorizeHeader xmlns="http://ws.idssasp.com/Members.asmx">
      <UserName>MyUsername</UserName>
      <Password>MyPassword</Password>
    </AuthorizeHeader>
  </soap:Header>
  <soap:Body>
    <GetCategoryList xmlns="http://ws.idssasp.com/Members.asmx" />
  </soap:Body>
</soap:Envelope>

使用正确的用户名和密码填写MyUsernameMyPassword。位于此处的URL:http://ws.idssasp.com/members.asmx?op=GetCategoryList&pn=0告诉我SOAP应该发送这样的请求:

POST /members.asmx HTTP/1.1
Host: ws.idssasp.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://ws.idssasp.com/Members.asmx/GetCategoryList"

<?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:Header>
    <AuthorizeHeader xmlns="http://ws.idssasp.com/Members.asmx">
      <UserName>string</UserName>
      <Password>string</Password>
    </AuthorizeHeader>
  </soap:Header>
  <soap:Body>
    <GetCategoryList xmlns="http://ws.idssasp.com/Members.asmx" />
  </soap:Body>
</soap:Envelope>

但当我浏览http://localhost/idss/Default.aspx时,我收到内部服务器错误(500)。我到底错在了什么?我需要添加名称空间吗?或者Default.aspx文件中某处的其他引​​用?如果是这样,需要哪一个?另外,我指的是(HttpWebRequest)WebRequest.Create("http://localhost/idss/Default.aspx");中的正确吗?

我这样做是否正确?我觉得这里缺少一些东西。

0 个答案:

没有答案