Web请求内容类型始终为text/html [英] Web request Content type is always text/html

查看:505
本文介绍了Web请求内容类型始终为text/html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个Web API. 此api将根据我正在发送的内容类型返回数据. 通常,它将返回html响应. 如果请求的Content-Type标头带有"application/json"标头,则它将返回json响应.

I have a web api I need to use. This api will return the data according the content type I'm sending. Normally it will return html response. If the request has Content-Type header with the 'application/json' header it will return a json response.

当我使用Postman尝试并添加内容类型标头(作为application/json)时,一切都很好. 但是,当使用C#WebRequest对象尝试它时,无论我使用的内容类型标头如何,我总是会得到html响应. 我曾经用提琴手来查看Web请求,内容类型始终是text/html.

When I try it using Postman and adding the content-type header (as application/json) everything is good. But when trying it using C# WebRequest object I'm always getting the html response regardless the content-type header I'm using. I've used fiddler to see the web request, and the content type is always text/html.

这是我的代码. 试过这个:

Here is my code. Tried this one:

    var webAddr = "https://xxxxxxxx.com/api/blabla";
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
        httpWebRequest.Method = "GET";

        httpWebRequest.Headers.Add("Authorization", "Basic xxxxxxxxxxx==");
        httpWebRequest.ContentType = "application/json; charset=utf-8";

        HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();


        Stream resStream = response.GetResponseStream();
        using (var reader = new StreamReader(resStream))
        {
            result = reader.ReadToEnd();
        }
        txtResult.Text = result;

也尝试过这个:

var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

        request.Method = Method.ToString();
        request.ContentLength = 0;
        request.ContentType = "application/json";

            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
            request.Headers.Add("Authorization", "Basic " + encoded);



        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                return null;
            }

            // grab the response
            using (var responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseValue = reader.ReadToEnd();
                    }
            }

仍然,每次内容类型都是text/html.

Still, every time, the content type is text/html.

预先感谢, 肖尔

推荐答案

您要设置的是Accept标头,而不是Content-Type. Accept指定要接收的数据类型. Content-Type用于指定您发送的数据类型.

What you want to do is set the Accept headers, not Content-Type. Accept specifies the types of data you want to receive. Content-Type is for specifying the type of data you're sending.

httpWebRequest.Accept = "application/json";

这篇关于Web请求内容类型始终为text/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆