JSON C#WebRequest返回错误400错误请求

时间:2016-03-10 13:55:31

标签: c# json httpwebrequest

以下代码在request.GetResponse()上抛出错误。它返回错误400错误请求。我尝试了不同的东西,但到目前为止没有任何成功。有什么我可以做的来获得有关错误400的更多细节吗?

string sURL = "https://testservice.mywebsite.be/games/1/players";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
        request.Method = "POST";
        request.Accept = "application/json";
        request.ContentType = "application/json";

        String sTimestamp = DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmmss");
        string sHMACmessage = string.Format("verb={0}&timestamp={1}&url={2}&playernumber={3}", request.Method.ToString().ToUpperInvariant(), sTimestamp, sURL, sPlayerNumber);
        byte[] bHash = HashHMAC(StringEncode("myprivatekey"), StringEncode(sHMACmessage));
        string sHash = HashEncode(bHash);

        request.Headers.Add("Timestamp", sTimestamp);
        request.Headers.Add("PlayerNumber", sPlayerNumber);
        request.Headers.Add("Authentication", String.Concat("mypublickey", ":", sHash));

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{\"playingYear\":\"2015\"}";
            streamWriter.Write(json);
        } 
        long length = 0;
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                length = response.ContentLength;
            }
        }
        catch (WebException ex)
        {
            throw;
        }

1 个答案:

答案 0 :(得分:0)

在测试服务中,请确保关闭以下内容以查看详细错误:

<?xml version="1.0"?>
<configuration>
<system.web>
    <customErrors mode="Off"/>
</system.web>
</configuration>

在尝试使用代码之前,请尝试使用像Fiddler这样的工具来查看它是否可以正常工作。