REST APIにデータを送信しようとしています。 APIのドキュメントでは、PATCHを使用してデータをJSONとして提供する必要があることがわかります。 APIはoAuth 2.0もコールする必要があるので、まずアクセストークンを取り出してapi url呼び出しに追加します。私が言う.GetResonse、上のエラーを取得し、 "(400)不正な要求を" tryブロックでHttpWebRequest PATCHメソッドとJSONが不正なリクエストを返す
public MyResponse HttpPatch(
string url,
string content,
Dictionary<string, string> headers,
string contentType = "application/json")
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var request = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(url));
if (request == null)
throw new ApplicationException(string.Format("Could not create the httprequest from the url:{0}", url));
request.Method = "PATCH";
foreach (var item in headers)
request.Headers.Add(item.Key, item.Value);
UTF8Encoding encoding = new UTF8Encoding();
var byteArray = Encoding.ASCII.GetBytes(content);
request.ContentLength = byteArray.Length;
request.ContentType = contentType;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
var response = (HttpWebResponse)request.GetResponse();
return new MyResponse(response);
}
catch (WebException ex)
{
HttpWebResponse errorResponse = (HttpWebResponse)ex.Response;
return new MyResponse(errorResponse);
}
}
:
私は、次のコードを持っています。私はこの方法に提供 値:= https://api.myserver.com/v1/users/1234?access_token=my_access_token
URL(MYSERVERとmy_access_tokenは私のコードで実際の値を持つ)
コンテンツ= LANG = FR &国籍= FR &国= FR & FIRST_NAME =ジョン& { "認可"、 "APIKEYのMYUSER:mykeyを"} 1つの要素とLAST_NAME = Doeの
ヘッダー=辞書 (MYUSERとにmykeyは、自分のコードで実際の値を持つ)
contentType = "application/json"
私は "bad request"エラーを説明することができないことが分かりませんか?このエラーの原因にはどのようなものがありますか?
私が使用するアクセストークンは正しいですが、エンドポイントのURLは正しいです。 メソッドの "PATCH"値がわかりません。どうすればいいですか? MSDNのドキュメントが可能な値でこれを言及していないので: https://msdn.microsoft.com/nl-be/library/system.net.httpwebrequest.method(v=vs.110).aspx
私の髪を引っ張る、2日間苦闘今では、うまくいけば、誰かが置くために私に私を与えるいくつかの良いポインタの光を表示することができ、作業のコールを取得します私は正しい軌道に乗っていますか?