2011-04-28 111 views
3

我試圖在這裏使用說明打了一個Web服務:從.NET發佈到REST Web服務?

http://help.seeclickfix.com/kb/api/creating-an-issue

我想出了下面的代碼:

 string paramContent = "api_key=afs684eas3ef86saef78s68aef68sae&issue[summary]=abeTest&issue[lat]=39.26252982783172&issue[lng]=-121.01738691329956&issue[address]=111 Abe St., Nevada City, CA"; 
     byte[] paramBytes = Encoding.UTF8.GetBytes(paramContent); 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://seeclickfix.com/api/issues.xml"); 
     req.Method = "POST"; 
     req.ContentLength = paramBytes.Length; 
     //req.ContentType = "application/x-www-form-urlencoded"; 

     using (Stream reqStream = req.GetRequestStream()) 
     { 
      reqStream.Write(paramBytes, 0, paramBytes.Length); 
     } 

     using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) //HERE! 
     { 
      if (resp.StatusCode != HttpStatusCode.OK) 
      { 
       string message = String.Format("POST failed. Received HTTP {0}", resp.StatusCode); 
       throw new ApplicationException(message); 
      } 

      StreamReader sr = new StreamReader(resp.GetResponseStream()); 
      string response = sr.ReadToEnd(); 

      Console.WriteLine(response + System.Environment.NewLine); 
     } 

但在與HERE!註釋的行則會引發錯誤:

The remote server returned an error: (500) Internal Server Error. 

任何人都可以看到任何問題,我試圖實現t他?

回答

3

您收到的500錯誤表明服務器存在問題,而不一定是您的代碼存在問題。您正在成功發送請求並收到回覆。

問題可能是服務器中的錯誤,或者服務器無法處理的請求內容問題。 (無論哪種方式,服務器無法提供有效的錯誤消息,就像他們的文檔所暗示的那樣)

您應該首先確保請求的內容是有效的。請參閱您發佈的seeclickfix網址上的示例。試着用捲曲直接張貼像他們展示,而是用自己的消息的內容,像這樣:

捲曲-v -d「API_KEY = afs684eas3ef86saef78s68aef68sae &問題[摘要] = abeTest &問題[LAT] = 39.26252982783172 &問題[ LNG] = - 121.01738691329956 &問題[地址] = 111安倍晉三街,內華達城,CA」 http://seeclickfix.com/api/issues.xml

我希望你還是會得到一個500錯誤(我只是嘗試了,我得到了一個500錯誤)。

底線,它看起來像他們的API是破碎的,而不是你的邏輯。

0

你沒有做錯任何事。我嘗試使用Fiddler發出請求,並返回相同的500狀態碼。

如果您傳遞的數據有問題,那麼它們應該返回一個4XX響應代碼。