簡體   English   中英

C#api響應和請求

[英]C# api responce and request

我目前有代碼

           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他們為此編寫的代碼是這樣的

    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. 

已經聲明的使用代碼示例是這樣的,有沒有人知道如何在c#中執行此操作或者我的原始代碼有什么問題?

   Usage Examples:
   CURL: curl -u user:password -d data="XML"         http://myanimelist.net/api/animelist/update/21.xml

編輯:當我laan myanimelist.net它顯示它還沒有更新,我相信我的用戶名和密碼憑據是正確的

編輯2:我現在添加了一個響應,出現錯誤“遠程服務器返回錯誤:(501)未實現。”

您實際上並沒有執行請求,因此一旦您完成了對請求流本身的寫入,請執行實際的Web請求:

  string result; 
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
       using (StreamReader reader = new StreamReader(response.GetResponseStream()))
       {
            result = reader.ReadToEnd();
       }
  }

此外,內容類型應為text/xmlapplication/xml - API可能會抱怨這一點。 仔細閱讀其API的文檔,確保您發送的內容是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM