GVKun编程网logo

尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException(尝试检查url的错误)

17

在本文中,我们将带你了解尝试测试HTTPPOST处理时出现HttpMediaTypeNotSupportedException在这篇文章中,我们将为您详细介绍尝试测试HTTPPOST处理时出现Http

在本文中,我们将带你了解尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException在这篇文章中,我们将为您详细介绍尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException的方方面面,并解答尝试检查url的错误常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content t...、ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应、c# – 尝试保存Emitted程序集时抛出NotSupportedException、How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?--转

本文目录一览:

尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException(尝试检查url的错误)

尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException(尝试检查url的错误)

如何解决尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException?

您应该将内容类型添加MediaType.APPLICATION_JSON到发布请求中,例如

this.mockmvc.perform(post("/rest/tests/")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"id\":10,\"width\":1000}"))
                .andDo(mockmvcResultHandlers.print())
                .andExpect(status().isOk());

解决方法

我正在尝试POST在spring框架中测试方法,但是我一直都在出错。

我首先尝试了这个测试:

this.mockMvc.perform(post("/rest/tests").
                            param("id","10").
                            param("width","25")
                            )
                .andDo(MockMvcResultHandlers.print())
                .andExpect(status().isOk());

并得到以下错误:

org.springframework.http.converter.HttpMessageNotReadableException

然后我尝试修改测试,如下所示:

this.mockMvc.perform(post("/rest/tests/").
                            content("{\"id\":10,\"width\":1000}"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(status().isOk());

但是出现了以下错误:
org.springframework.web.HttpMediaTypeNotSupportedException

我的控制器是:

@Controller
@RequestMapping("/rest/tests")
public class TestController {

    @Autowired
    private ITestService testService;

    @RequestMapping(value="",method=RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.OK)
    public void add(@RequestBody Test test)
    {
        testService.save(test);
    }
}

其中Testclass有两个字段成员:idwidth。简而言之,我无法为控制器设置参数。

设置参数的正确方法是什么?

ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content t...

ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content t...

ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedException: Content type ''application/x-www-form-urlencoded;charset=UTF-8'' not supported的错误。

解决办法是设置ajax的contentType为"application/json"

 1 $.ajax({
 2         $.ajax({
 3             url : urladdress,
 4             type : "POST",
 5             dataType : ''JSON'',
 6             data :  JSON.stringify(JsonObj),
 7 
 8             //设置请求的contentType为"application/json"
 9             contentType: "application/json",
10 
11             success : function(response) {
12                 //处理返回的响应结果
13             }
14 });

通过上述设置可以成功解决一些项目出现的这个问题,其他项目可能会有不一样或还有其他的解决办法。。。

 

ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应

ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应

将表单 POST HTTP 请求 ( Content-Type: application/x-www-form-urlencoded)
发送到以下控制器会导致 HTTP 415 Unsupported Media Type 响应。

public class MyController : Controller{    [HttpPost]    public async Task<IActionResult> Submit([FromBody] MyModel model)    {        //...    }}

表单发布 HTTP 标头:

POST /submit HTTP/1.1Host: example.com:1337Connection: keep-aliveContent-Length: 219Pragma: no-cacheCache-Control: no-cacheOrigin: https://example.com:1337Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36Content-Type: application/x-www-form-urlencodedAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Referer: https://example.com:1337/submitAccept-Encoding: gzip, deflate, brAccept-Language: en-US,en;q=0.8,nl;q=0.6

这曾经在 .NET 4.6 上与 ASP.NET MVC 5 一起使用。

答案1

小编典典

对于表单,使用 [FromForm] 属性而不是[FromBody]属性。

以下控制器适用于 ASP.NET Core 1.1:

public class MyController : Controller{    [HttpPost]    public async Task<IActionResult> Submit([FromForm] MyModel model)    {        //...    }}

注意:[FromXxx]如果您的控制器使用[ApiController]. 对于普通视图控制器,它可以省略。

c# – 尝试保存Emitted程序集时抛出NotSupportedException

c# – 尝试保存Emitted程序集时抛出NotSupportedException

http://pastebin.com/n6G1jTHQ
我的代码创建了一个新的程序集和模块,然后发出一个类SWT(在一个新的程序集中),它应该与SWTTFieldsclass完全相同.
第137行,将程序集/模块保存到.dll,myAsmBuilder.Save(“ModuleOne.dll”);抛出异常:

An unhandled exception of type ‘System.NotSupportedException’ occurred
in mscorlib.dll Additional information: Type ‘SWT’ was not completed.

为什么我收到此错误?我的课程缺少什么?

解决方法

在保存文件之前,必须为每个TypeBuilder调用 TypeBuilder.CreateType Method.

How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?--转

How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?--转

http://dotnetstock.com/technical/http-status-code-505-http-version-not-supported/

The reason for the error 505 is,the Web server does not support,or refuses to support,the HTTP protocol version specified by the client  in the HTTP request data stream sent to the server.Here let us see How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?

There will be mainly 3 reason for this error.

1. The request you are sending to the server from the client browser,it is not in the form which can be understand by the server.2. A third-party product is interfering with Internet Explorer.3. The HTTP 1.1 setting is enabled.

How to fix the error 505- HTTP Version Not Supported?

As a first step you just check the same url from an updated browser.

verify the HTTP request,whether this request contain any kind of junk character is there in your request,or any white spaces are creating the issue. To overcome this white space issue you can use the urlencode() function before calling the server.

If the issue is happening only in you browser,try this.

1. In Internet Explorer,click Internet Options on the Tools menu.2. In the Internet Options dialog Box,click the Advanced tab.3. Click to clear the HTTP 1.1 check Box,and then click OK.

4. Test to determine if the issue is resolved. If the issue is resolved,do not complete the remaining steps. If the issue is not resolved,go to the next step.5. In Internet Explorer,click Internet Options on the Tools menu.6. In the Internet Options dialog Box,click the Advanced tab.7. Click to clear the Show friendly HTTP error messages check Box.8. Try to view a Web page to reproduce the error message and to receive a new,more descriptive error message. This new error message may be similar to the following:HTTP version not supported by program name.Note the name of the program in this new error message.9. The program name that you noted in step 8 may be the cause of this issue. Either remove this program,or contact the manufacturer of the program to inquire about the availability of a fix for this issue.

关于尝试测试HTTP POST处理时出现HttpMediaTypeNotSupportedException尝试检查url的错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content t...、ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应、c# – 尝试保存Emitted程序集时抛出NotSupportedException、How to fix “HTTP Status Code 505 – HTTP Version Not Supported” error?--转等相关知识的信息别忘了在本站进行查找喔。

本文标签: