0

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);
        }

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. 

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

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

Edit 2 : I have now added a response which comes up with the error "The remote server returned an error: (501) Not Implemented."

Froodle
  • 339
  • 2
  • 5
  • 15
  • When i lauch myanimelist.net it shows that it has not been updated, i am sure that my username and password credentials are correct – Froodle Aug 13 '13 at 12:58

1 Answers1

2

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();
       }
  }

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.

Community
  • 1
  • 1
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • cheers, i now get the error "The remote server returned an error: (501) Not Implemented." – Froodle Aug 13 '13 at 13:04
  • 1
    @FroodleStirling, I've made some edits - but I think the rest of it may be beyond the scope of the question and getting a bit too localized. – Moo-Juice Aug 13 '13 at 13:07
  • Tried changing the Content type but with no success, cheers anyway – Froodle Aug 13 '13 at 13:09