2016-01-26 67 views
0

我在c#中有一個代碼,但是我得到下一個錯誤:「(400)不正確的請求」。在REST API中插入HP ALM的要求#c#

HttpWebRequest myWebRequest2 = (HttpWebRequest)WebRequest.Create("http://myURL/qcbin/rest/domains/DEFAULT/projects/Mercury/requirements"); 
      myWebRequest2.Method = "POST"; 
      myWebRequest2.Accept = "application/json"; 
      myWebRequest2.ContentType = "application/json"; 

      myWebRequest2.CookieContainer = new CookieContainer(); 
      myWebRequest2.CookieContainer.Add(webResponse.Cookies[0]); 
      myWebRequest2.CookieContainer.Add(webResponse.Cookies[1]); 
      myWebRequest2.CookieContainer.Add(webResponse.Cookies[2]); 
      myWebRequest2.CookieContainer.Add(webResponse.Cookies[3]); 


      StreamWriter streamWriter = new StreamWriter(myWebRequest2.GetRequestStream()); 
      string json = "{ \"type-id\": 3,\"name\": \"MyPrueba\"}"; 

      streamWriter.Write(json); 
      streamWriter.Flush(); 
      streamWriter.Close(); 

      webResponse = (HttpWebResponse)myWebRequest2.GetResponse(); 

我行webResponse = (HttpWebResponse)myWebRequest2.GetResponse();

+0

「400」 是一個HTTP錯誤,這是由服務器在 「myURL」 返回。所以這取決於Web服務期望的內容,以及這與您發送的內容有什麼不同。 –

+1

您應該創建一個CookieContainer並將其分配給您執行的每個Web請求。您在這裏複製cookie的方式是失敗的祕訣。還要安裝一個本地代理,如fiddler,以便您可以調試http請求。 – rene

回答

0

關於Create an Entity documentation得到的錯誤,也有一些問題,JSON的結構,你要發送。

這種結構

{ 
    "Fields":[{ 
     "Name":"type-id", 
     "values":[{"value":"3"}] 
    },{ 
     "Name":"name", 
     "values":[{"value":"MyPrueba"}] 
    }] 
} 

應當被用來代替這一個

{ 
    "type-id":3, 
    "name":"MyPrueba" 
}