我试图将某些xml发布到http://api.opensubtitles.org:80/xml-rpc,但它无效。如果我使用https://www.hurl.it/,我会收到我期待的回复,但不能得到我的C#。这是xml:
<?xml version='1.0' encoding='utf-8'?>
<methodCall>
<methodName>LogIn</methodName>
<param>
<value><string></string></value>
</param>
<param>
<value><string></string></value>
</param>
<param>
<value><string></string></value>
</param>
<param>
<value>
<string>OSTestUserAgent</string>
</value>
</param>
这是我的C#:
public static async Task<string> HttpRequest(string xml)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.opensubtitles.org:80/xml-rpc");
request.Method = "POST";
using (StreamWriter dataStream = new StreamWriter(await request.GetRequestStreamAsync()))
{
dataStream.Write(xml);
dataStream.Dispose();
}
string result = null;
using (WebResponse response = await request.GetResponseAsync())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
result = reader.ReadToEnd();
}
}
Debug.WriteLine(result);
return result;
}
我收到了一个&#34;解析错误。没有很好的形成&#34;从服务器。究竟是什么问题?
答案 0 :(得分:1)
您忘记按照XMLRPC specification将参数列表放在<params></params>
标记中。这段代码很好用:
var myxml =
@"<?xml version='1.0' encoding='utf-8'?>
<methodCall>
<methodName>LogIn</methodName>
<params>
<param>
<value><string></string></value>
</param>
<param>
<value><string></string></value>
</param>
<param>
<value><string></string></value>
</param>
<param>
<value>
<string>OSTestUserAgent</string>
</value>
</param>
</params>
</methodCall>";
var request = (HttpWebRequest)WebRequest.Create("http://api.opensubtitles.org:80/xml-rpc");
request.Method = "POST";
using (var dataStream = new StreamWriter(await request.GetRequestStreamAsync()))
{
dataStream.Write(myxml);
dataStream.Dispose();
}
string result = null;
using (var response = await request.GetResponseAsync())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.ASCII))
{
result = reader.ReadToEnd();
}
Debug.WriteLine(result);