0

我收到了一个错误,并按照我收到的关于如何编码我的请求的 API 手册进行操作。下面是我的要求..

string url = "[My url to send request to]";

string xmlrequest = "<serv_request><head><securityContext><account>[account]</account><key>[my account key]</key></securityContext></head><body><username>[my user name]</username></body></serv_request>";

NameValueCollection nvc = new NameValueCollection();
nvc.Add("xml", Server.UrlEncode(xmlrequest));


WebClient client = new WebClient();

byte[] byteresponse = client.UploadValues(url, nvc);

string xmlresponse = client.Encoding.GetString(byteresponse);

我收到了错误的回复。在文档的顶层无效。

编辑..添加提供给我的 API 手册中的说明..

string url = " http://[domain_name]/_gateway/api/[filename].asp"; 

// formulate the XML request here 

string xmlrequest = "<serv_request>...</serv_request>";  

NameValueCollection nvc = new NameValueCollection(); 
nvc.Add("xml", Server.UrlEncode(xmlrequest)); 

WebClient client = new WebClient(); 
byte[] byteresponse = client.UploadValues(url, nvc); 

string xmlresponse = client.Encoding.GetString(byteresponse); 
4

2 回答 2

1

我设法解决了这个问题。我没有传递包含所有 xml 的字符串并使用 Server.UrlEncode,而是使用了 XmlWriter 类和 StringBuilder。你所有的回复都很有帮助,也很受欢迎,我会投票给他们,但我还不能做那个功能。也许这将在未来对其他人有所帮助。谢谢

        XmlWriter writer;
        StringBuilder sb = new StringBuilder();
        writer = XmlWriter.Create(sb);
        writer.WriteStartElement("serv_request");
        writer.WriteStartElement("head");
        writer.WriteStartElement("securityContext");
        writer.WriteStartElement("account");
        writer.WriteString("MyAccountName");
        writer.WriteEndElement();
        writer.WriteStartElement("key");
        writer.WriteString("MyKey");
        writer.WriteEndElement(); //closes Key Element
        writer.WriteEndElement(); // closes securityContent
        writer.WriteEndElement(); //closes head
        writer.WriteStartElement("body");
        writer.WriteStartElement("username");
        writer.WriteString("MyUserName");
        writer.WriteEndElement(); // closes username
        writer.WriteEndElement(); //closes body
        writer.WriteEndElement(); //closes serv_request
        writer.Close();

        NameValueCollection nvc = new NameValueCollection();           
        nvc.Add("xml", sb.ToString());          

        WebClient client = new WebClient();
        byte[] byteresponse = client.UploadValues(url, nvc);

        string xmlresponse = client.Encoding.GetString(byteresponse);
于 2013-11-23T05:00:31.193 回答
0

听起来服务器没有用正确的 xml 响应,它可能返回一个空字符串或任何不是格式良好的 XML 的文本。

尝试捕获您正在发送的 xml,并使用诸如 soapUI 之类的工具来发送请求并查看它给您的响应。

也可以试试这个而不是你的最后一行代码:

字符串 xmlresponse = System.Text.Encoding.UTF8.GetString(byteresponse);

于 2013-11-22T21:08:00.173 回答