C#API的反响,请求 [英] C# api responce and request
问题描述
我目前拥有的code
I currently have the code
try
{
string url = "http://myanimelist.net/api/animelist/update/" + "6.xml";
WebRequest request = WebRequest.Create(url);
request.ContentType = "xml/text";
request.Method = "POST";
request.Credentials = new NetworkCredential("username", "password");
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<episode>4</episode>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
MessageBox.Show("Updated");
}
catch (Exception s)
{
MessageBox.Show(s.Message);
}
我试图做的数据发送到myanimelist.net
他们为编写的code是这个
I am trying do send data to myanimelist.net The code they have written for is this
URL: http://myanimelist.net/api/animelist/update/id.xml
Formats: xml
HTTP Method(s): POST
Requires Authentication:true
Parameters:
id. Required. The id of the anime to update.
Example: http://myanimelist.net/api/animelist/update/21.xml
data. Required. A parameter specified as 'data' must be passed. It must contain anime values in XML format.
Response: 'Updated' or detailed error message.
在已经指出的使用code例子是这样的,没有人知道如何做到这一点在C#或者什么是错的我原来的code?
The usage code example the have stated is this, does anyone know how to do this in c# or what was wrong with my original code?
Usage Examples:
CURL: curl -u user:password -d data="XML" http://myanimelist.net/api/animelist/update/21.xml
编辑:当我劳克myanimelist.net它表明,它一直没有更新,我相信,我的用户名和密码凭据是正确的。
edit: When i lauch myanimelist.net it shows that it has not been updated, i am sure that my username and password credentials are correct
编辑2:我现在已经添加了与错误出现响应
远程服务器返回错误:(501)未实现
Edit 2 : I have now added a response which comes up with the error "The remote server returned an error: (501) Not Implemented."
推荐答案
您实际上并不执行请求,所以一旦你完成写入请求的流本身,执行实际的web请求:
You're not actually performing the request, so once you're done writing to the request stream itself, perform the actual web request:
string result;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
另外,内容类型应<一href=\"http://stackoverflow.com/questions/2965587/valid-content-type-for-xml-html-and-xhtml-documents\"><$c$c>text/xml$c$c>或应用程序/ XML
- 该API可抱怨的。请仔细阅读文档的API,并确保你要发送是正确的。
Also, the content type should be text/xml
or application/xml
- the API may be complaining about that. Read the documentation for their API carefully and ensure what you're sending is correct.
这篇关于C#API的反响,请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!