2016-01-23 106 views
0

我本來就問我是試着寫,然後發現ASP.net網絡API是更適合我的需要,由於對這裏的一些反饋一個WCF Web服務的問題。C#的Web API REST服務POST

我現在已經找到了一個很好的教程,告訴我如何使用Web API,它工作得很好幾乎開箱創建一個簡單的REST服務。

我的問題

我有我的REST服務服務器POST方法:

// POST api/values/5 
    public string Post([FromBody]string value) 
    { 
     return "Putting value: " + value; 
    } 

我可以張貼到此使用海報,也是我的C#的客戶端代碼。

但是我不明白的位爲我爲什麼要在前面加上一個「=」號的POST數據,這樣記載:「=這是我的數據,實際上是一個JSON字符串」;而不僅僅是發送:「這是我的數據,它實際上是一個JSON字符串」;

,討論到REST服務我的C#的客戶如下記載:

public string SendPOSTRequest(string sFunction, string sData) 
    { 
     string sResponse = string.Empty; 

     // Create the request string using the data provided 
     Uri uriRequest = GetFormRequest(m_sWebServiceURL, sFunction, string.Empty); 
     // Data to post 
     string sPostData = "=" + sData; 
     // The Http Request obj 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriRequest); 
     request.Method = m_VERB_POST; 
     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); 
     Byte[] byteArray = encoding.GetBytes(sPostData); 
     request.ContentLength = byteArray.Length; 
     request.ContentType = m_APPLICATION_FORM_URLENCODED;     

     try 
     { 
      using (Stream dataStream = request.GetRequestStream()) 
      { 
       dataStream.Write(byteArray, 0, byteArray.Length); 
      } 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       using (Stream stream = response.GetResponseStream()) 
       { 
        StreamReader reader = new StreamReader(stream, Encoding.UTF8); 
        sResponse = reader.ReadToEnd(); 
       }      
      } 
     } 
     catch (WebException ex) 
     { 
      //Log exception 
     } 

     return sResponse; 
    } 

    private static Uri GetFormRequest(string sURL, string sFunction, string sParam) 
    { 
     StringBuilder sbRequest = new StringBuilder(); 
     sbRequest.Append(sURL); 

     if ((!sURL.EndsWith("/") && 
      (!string.IsNullOrEmpty(sFunction))))    
     { 
      sbRequest.Append("/"); 
     } 
     sbRequest.Append(sFunction); 

     if ((!sFunction.EndsWith("/") && 
      (!string.IsNullOrEmpty(sParam)))) 
     { 
      sbRequest.Append("/"); 
     } 
     sbRequest.Append(sParam); 

     return new Uri(sbRequest.ToString()); 
    } 

是任何人能解釋爲什麼我要在前面加上「=」號在上面的代碼(string sPostData = "=" + sData;)?

非常感謝提前!

+1

Whay你不使用restsharp這樣的東西嗎? http://restsharp.org/ –

+0

@KosalaW http://i.imgur.com/ybWKjSM.png:D – DSF

+0

嗨Kosala W,謝謝你。說實話,我沒有看這個,但也許我應該有。我的工作是編寫與最終由第三方編寫的服務器代碼接口的客戶端代碼。我只需要提供API的測試工具和指南。 考慮到這一點,你是否對上述內容有任何反饋意見,因爲在POST數據前加上'='符號似乎是錯誤的,但如果必須,我會我只想多瞭解一點? 感謝您使用m_APPLICATION_FORM_URLENCODED – Yos

回答

1

內容類型x-www-form-urlencoded是一個關鍵值格式。使用表單體,您只能從請求主體讀取一個簡單類型。作爲一個名字,但在這種情況下不允許,你必須在等號前面加上前綴值來表示沒有名字。

然而,你應該接受你的網絡API控制器動作的身體內簡單類型瘦了。

如果您試圖在HttpPost/HttpPut正文中傳遞數據而不直接實現您自己的MediaTypeFormatter(這不可能是可靠的),那麼您僅限於一種簡單類型。創建一個輕量級的複雜類型通常更加可取,並且與其他內容類型(如application/json)更容易互操作。

+0

嘿Jonathon,非常感謝你爲我解釋了這個解釋。現在我明白了爲什麼,我可以考慮在我的約束範圍內解決問題的最佳方法。我接受你對使用簡單類型的看法。再次感謝! – Yos