WebAPI POST,服务总是接收null

本文关键字:null POST 服务 WebAPI | 更新日期: 2023-09-27 17:52:39

关于这个问题我已经找了几个小时了,但我似乎找不到任何答案

我已经建立了一个ASP。. NET WebAPI接受JSON GET/POST请求,当我使用fiddler或高级rest客户端扩展for google chrome时,它可以工作A1。

现在我必须采取一个现有的android应用程序,并修改它与我的WebAPI工作。我已经清除了所有的垃圾,我不需要使它更容易,但我仍然不能发布。GET工作正常,我收到我的响应字符串,一切都很好,但是POST返回一个400错误的请求。

我的webApi控制器需要一个ProblemReports对象。问题报告是由我做的,它是这样的:

    public class ProblemReports
{
    [Key]
    public int problem_id { get; set; }
    [Required]
    public Nullable<int> request_type { get; set; }
    [Required]
    public Nullable<int> problem_type_id { get; set; }
    public String picture_url { get; set; }
    public contact_information contact_information { get; set; }
    public problem_location problem_location { get; set; }
    public bool dog_on_property { get; set; }
}
与子类

    public class problem_location
{
    public String street { get; set; }
    public String city { get; set; }
    public int relative_position_id { get; set; }
    public double longitude { get; set; }
    public double latitude { get; set; }
}

    public class contact_information
{
    public String name { get; set; }
    public String phone { get; set; }
}

这是我的android POST在我的服务处理程序类的代码有趣的是,我可以在将json字符串发送到StringEntity之前设置一个断点,复制原始json字符串,将其粘贴到BODY部分高级rest客户端,填充标题,点击post和boom: Response is 200 OK

如果我在控制器上断点WebAPI,我可以看到,当通过高级rest客户端发送请求时,我可以访问problemreports,但如果我在android应用程序的请求上中断它,problemreports为null

public static String POST(String url, ProblemReports report) {
    InputStream inputStream = null;
    String result = "";
    if (report.picture_url == null)
    {
        report.picture_url = "no picture";
    }
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        String json = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("request_type", report.request_type);
        jsonObject.accumulate("problem_type_id", report.problem_type_id);
        jsonObject.accumulate("picture_url", report.picture_url);
        JSONObject contact_informationObject = new JSONObject();
        contact_informationObject.accumulate("name",
                report.contact_information.name);
        contact_informationObject.accumulate("phone",
                report.contact_information.phone);
        jsonObject.accumulate("contact_information",
                contact_informationObject);
        JSONObject problem_locationObject = new JSONObject();
        problem_locationObject.accumulate("street",
                report.problem_location.street);
        problem_locationObject.accumulate("city",
                report.problem_location.city);
        problem_locationObject.accumulate("latitude",
                report.problem_location.latitude);
        problem_locationObject.accumulate("longitude",
                report.problem_location.longitude);
        problem_locationObject.accumulate("relative_position_id",
                report.problem_location.relative_position_id);
        jsonObject.accumulate("problem_location", problem_locationObject);
        jsonObject.accumulate("dog_on_property", report.dog_on_property);
        json = jsonObject.toString();
        //String otherJson = "{ProblemReports: " + json + "}";
        //I saw on the web to add ProblemReports: but it doensn't work
        StringEntity se = new StringEntity(json);
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json; charset=utf-8");
        httpPost.setHeader(
                "Authorization",
                "Bearer TokenRemovedBecauseUseless");

        HttpResponse httpResponse = httpclient.execute(httpPost);
        inputStream = httpResponse.getEntity().getContent();
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "InputStream convert fail!!";
    } catch (Exception e) {
        return e.getCause().toString();
    }
    return result;
}

这是我的应用程序的原始JSON字符串。实际的字符串在Fiddler或高级rest客户端上工作良好

{
"contact_information":
{
    "phone":"6666666666",
    "name":"fiber"
},
"dog_on_property":false,
"problem_type_id":3,
"request_type":1,
"problem_location":
{
    "longitude":1234,
    "latitude":1234,
    "relative_position_id":0,
    "city":"Montreal, QC A1A 1A1",
    "street":"0000 René-Lévesque Blvd"
}

}

我不知道这里是怎么回事,任何帮助/提示/建议都可以。我设置我的控制器抛出任何错误,我得到的唯一的东西是400 BAD REQUEST

这里是控制器的post部分

 // POST api/ProblemReports
    [ResponseType(typeof(ProblemReports))]
    public IHttpActionResult PostProblemReports([FromBody]ProblemReports problemreports)
    {
        var allErrors = ModelState.Values.SelectMany(v => v.Errors);
        if (!ModelState.IsValid)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
          //  return BadRequest(ModelState);
        }
        try
        {
            db.ProblemReports.Add(problemreports);
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            return Ok(ex);
        }

        //return CreatedAtRoute("DefaultApi", new { id = problemreports.problem_id }, problemreports);
        ReturnID ri = new ReturnID(problemreports.problem_id);
        return Ok(ri);
    }

WebAPI POST,服务总是接收null

找到了我问题的答案。花了4个小时才找到这个小错误,但它是这样的:

我有

一行
 httpPost.setHeader("Content-type", "application/json; charset=utf-8")

和字符串实体

 StringEntity se = new StringEntity(json);

必须是

 httpPost.setHeader("Content-type", "application/json") 

 StringEntity se = new StringEntity(json, "UTF-8");

一切都很好,抱歉打扰了大家感谢阅读,祝你有一个美好的一天