在C#中执行帖子请求会导致奇怪的输出



我正在向几个发音序列进行发布请求。这些服务器返回所有JSON,除了一个。其中一个返回这样的数据:1FE2 80B9 0800 0000 0000 0400 C3AC C2BD。

如果我使用REST客户端进行相同的操作

我使用以下代码来执行此操作:

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;
        }

我真的不能给您服务器URL,因为它们是私人的。有人知道为什么要获取这些数据而不是有效的JSON?

嗨,请在计算机中安装提琴手工具,然后检查REST客户端生成的请求标头。并捕获.NET客户端生成的请求标头。然后比较两种请求格式以找到解决方案

相关内容

  • 没有找到相关文章

最新更新