请求为JSON时,RestSharp XML DOCTYPE错误

时间:2018-05-15 16:03:57

标签: c# rest restsharp

我一直在尝试使用RestSharp Library来发出请求,但是当我尝试发出POST请求时,这个错误不断出现!!

enter image description here

以下是代码:

private readonly string contentType = "application/json";


try
{
    var restClient = new RestClient(new Uri(InvoiceExpressURLS.URL_createClient));

    string clientJsonRequest =
    JsonConvert.SerializeObject(
        JsonConvert.DeserializeObject("{\"client\":" + JsonConvert.SerializeObject(newClient) + "}")
    );

    var request = new RestRequest(Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.AddHeader("Content-Type", contentType);
    request.AddJsonBody(clientJsonRequest);

    var restResponse = await restClient.ExecuteTaskAsync<object>(request);
    if (restResponse.IsSuccessful)
    {
        response.responseObject = restResponse.Data;
        response.responseMessage = string.Format("The client \"{0}\" was successfuly created.", newClient.name);
    }
    else
    {
        response.responseStatus = restResponse.StatusCode;
        response.responseMessage = restResponse.ErrorMessage;
    }
}
catch (Exception e)
{
    response.responseStatus = HttpStatusCode.InternalServerError;
    response.responseMessage = e.Message;
    response.responseObject = e;
}

0 个答案:

没有答案