简体   繁体   中英

HTTP Error 406 after execting a restsharp request

I'm trying to use RestSharp to connect to a server. but I get HTTP Error 406 Not acceptable . Here is the web service response:

{"metaInfo":{"ErrorString":" ","ErrorCode":" ","Result":true},"userModel":
{"UserId":"123qwe","UserName":"aaa","FirstName":"ALi","LastName":"TbT"}}

The rest service is actually a simple php file that I created for test:

<?php
$a = array("metaInfo"=>array("ErrorString"=>" ","ErrorCode"=>" ","Result"=>true),"userModel"=>array("UserId"=>"123qwe","UserName"=>"aaa","FirstName"=>"ALi","LastName"=>"TbT"));
echo json_encode($a);
?>

And Here is the RegisterResult and MetaInfo classes:

public class RegisterResult
    {
        public MetaInfo metaInfo { get; set; }
    }
public class MetaInfo
    {
        public string ErrorString { get; set; }
        public string ErrorCode { get; set; }
        public bool Result { get; set; }
    }

The code that runs the rest request is as follows:

var client = new RestClient(Configuration.PortalUri);
var request = new RestRequest(requestUri, HttpMethod.Post);
var asyncHandle = await client.Execute<RegisterResult>(request);

Could you please tell me what the problem is? I think something is wrong with headers or encoding or something like that. Am I right?

This error is specific to the way the programmers have created the service you are calling. There's nothing wrong with your code from a C# perspective, but the way the service author has chosen to implement their service doesn't like something in the service call. You will have to refer to their documentation or ask one of their programmers.

Here's an explanation of http error 406 (taken from http://www.checkupdown.com/status/E406.html ):

Fixing 406 errors - general

This error occurs very infrequently in Web browsers, because most browsers will accept any data returned from the Web server.

If the client is not a Web browser, then anyone can only investigate the problem by looking at the Accept headers generated by the client system

Consider these headers to modify:

•Accept: The MIME types accepted by the client. For example, a browser may only accept back types of data (HTML files, GIF files etc.) it knows how to process.
•Accept-Charset: The character sets accepted by the client.
•Accept-Encoding: The data encoding accepted by the client eg the file formats it understands. •Accept-Language: The natural languages (English, German etc.) accepted by the client. •Accept-Ranges: Whether the client accepts ranges of bytes from the resource ie a portion of the resource.

For example, you may need to add this to your request:

request.AddHeader("Accept", "application/json");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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