私がWrapした第三者Apiのリソースに対するOAuthアクセストークンリクエストを作成している場合、たとえばすべての顧客事例データを取得します。ParamsなしでGET用のバッファが必要ですか?
私はデータをリクエストしているだけなので、このGETに必要なクエリーストリングはありませんが、私の質問は、何らかの種類のリクエストストリーム(バイトデータ)を指定する必要があるのでしょうか、要求オブジェクトによって-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