C# 调用REST Web服务时不支持的媒体类型
我正在调用一个REST web服务,它为我提供了这个文档C# 调用REST Web服务时不支持的媒体类型,c#,rest,C#,Rest,我正在调用一个REST web服务,它为我提供了这个文档 HTTP Method: POST Path: /commit/{path}/add-node Response Status 200, 302, 403, 404, 409, 503 Form Parameters - name : attribute name - message : commit message 基于此文档。我编写了以下C#代码 我也试过了 string restUrl = we
HTTP Method: POST
Path: /commit/{path}/add-node
Response Status 200, 302, 403, 404, 409, 503
Form Parameters
- name : attribute name
- message : commit message
基于此文档。我编写了以下C#代码
我也试过了
string restUrl = webServiceurl + "/commit/" + path + "/add-node";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restUrl);
request.Method = "POST";
request.ContentType = @"application/json";
var param = new { name = nodeName, message = commitMessage };
Stream reqStream = null;
string output = null;
try {
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(
JsonConvert.SerializeObject(param)
);
request.ContentLength = buffer.Length;
reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
using (WebResponse response = request.GetResponse()) {
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
output = reader.ReadToEnd();
}
}
} catch (Exception ex) {
.....
}
不幸的是,在这两种情况下,我都得到415个不支持的媒体类型。我的代码有什么问题
web服务是用Java编写的基于REST的web服务。根据ContentType属性,Java web服务可能不支持该服务。您确定它接受application/json吗 您能够使用Fiddler或类似工具调用JavaWeb服务吗?
string restUrl = webServiceurl + "/commit/" + path + "/add-node";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restUrl);
request.Method = "POST";
request.ContentType = @"application/json";
var param = new { name = nodeName, message = commitMessage };
Stream reqStream = null;
string output = null;
try {
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(
JsonConvert.SerializeObject(param)
);
request.ContentLength = buffer.Length;
reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
using (WebResponse response = request.GetResponse()) {
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
output = reader.ReadToEnd();
}
}
} catch (Exception ex) {
.....
}