如果我爲我封裝的第三方Api的資源進行OAuth訪問令牌請求,以獲取所有客戶案例數據。你需要一個無參數GET的緩衝區嗎?
這個GET沒有querystrings所需,因爲我只是請求數據回來,但我的問題是,我仍然需要指定某種請求流(字節數據),或者只是假設ContentLength應該留下out並被Request對象讀爲-1?
例如我不必擔心設置ContentLength或緩衝區的做法。我只需要使用流讀取器來獲取返回的.json或API發回給我的正確內容?
HttpWebResponse response;
Stream dataStream; // data returned from the response
byte[] buffer = null; // data to send in the request body
// FYI the "data" variable is just an incoming param to my send method, a string if I want to send data in the request (i.e. json, xml, whatever I am sending if needed)
if (!string.IsNullOrEmpty(data.Trim())) buffer = Encoding.UTF8.GetBytes(data);
// the resourceUrl variable I'm specifying for example is "ttp://someThirdPartyApi.com/api/v1/cases.json"
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resourceUrl);
// I then add an authorization header to the resonse.Headers (not shown here)
request.ServicePoint.Expect100Continue = false;
request.PreAuthenticate = true;
// do we have any data to send in the request -body-??
if (buffer != null && buffer.Any())
{
request.ContentLength = buffer.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
}
// no data to send, just get the data returned in the response using a StreamReader
你試過了嗎?發生了什麼? – Oded 2012-03-04 09:00:45