如果我为我封装的第三方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