简体   繁体   English

调用REST Web服务时不支持的媒体类型

[英]UnSupported Media Type when Calling REST Web Service

I am calling a REST web service which has given me this documentation 我正在打电话给我的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 

Based on this documentation. 基于此文档。 I have written following C# code. 我已经编写了以下C#代码。

  string restUrl = webServiceurl + "/commit/" + path + "/add-node";
  restUrl = restUrl + "?name=" + nodeName + "&message=" + commitMessage;
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restUrl);
  request.Method = "POST";
  request.ContentType = @"application/json";
    using (WebResponse response = request.GetResponse()) {
      using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
        output = reader.ReadToEnd();
      }
    }

I also tried 我也试过

  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) {
      .....
  }

Unfortunately in both cases, I get 415 Unsupported Media Type in both cases. 不幸的是,在两种情况下,我都得到415不支持的媒体类型。 What is wrong with my code? 我的代码有什么问题?

The web Services is a REST based web service written in Java. Web服务是用Java编写的基于REST的Web服务。

According to this forum post the ContentType property may not be supported from the Java web service. 根据此论坛帖子 ,Java Web服务可能不支持ContentType属性。 Are you sure it accepts application/json? 您确定它接受application / json吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM