我正在向几个服务器发帖子。这些服务器返回除一个以外的所有json。其中一个返回如下数据:1fe2 80b9 0800 0000 0000 0400 c3ac c2bd。
如果我对休息客户端做同样的事情,我会从所有服务器获得有效的Json但是当我在c#中执行它时,其中一个服务器会返回这种数据。
我使用以下代码执行此操作:
public static string MakeRequest(string url, string requestBody, string methodName)
{
string result = "";
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
result = client.UploadString(string.Format("{0}{1}", url, methodName), "POST", requestBody);
}
return result;
}
我也试过这个(旧代码):
public static string MakeRequestA(string url, string requestBody, string methodName)
{
byte[] postBytes = Encoding.ASCII.GetBytes(requestBody);
var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", url, methodName));
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "text/plain";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
var dataStream = response.GetResponseStream ();
var reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
reader.Close ();
dataStream.Close ();
response.Close ();
return responseFromServer;
}
我无法真正为您提供服务器网址,因为它们是私有的。有人知道为什么我得到这些数据而不是有效的json?
答案 0 :(得分:0)
请在您的计算机上安装fiddler工具,然后检查REST客户端生成的请求标头。并捕获.net客户端生成的请求头。然后比较两种请求格式以找到解决方案