简体   繁体   English

标头错误中的 HTTP 主机属性(主机属性)

[英]HTTP host property in header error (host property )

Dears iam trying to do a POST HTTP now iam using Postman -on postman it working fine without any error and iam getting data but when i wite the code iam getting and error亲爱的,我现在正在尝试做一个 POST HTTP,我现在使用 Postman -on postman 它工作正常没有任何错误并且我正在获取数据但是当我看到代码时我正在获取和错误

The remote server returned an error: (400) Bad Request远程服务器返回错误:(400) Bad Request

i compared the code i found the postman adds some headers like User-Agent,Accept,... i unchecked them all to see which one is affecting i found on postman that the host property if i uncheck it ,it give an error all other are not needed so but as i know that the host is calculated at runtime ,i tried to put the host in the code but no luck can anyone help this is my code我比较了代码,我发现邮递员添加了一些标头,例如 User-Agent、Accept,...不需要,但我知道主机是在运行时计算的,我尝试将主机放入代码中,但没有运气任何人都可以帮助这是我的代码

  var request = (HttpWebRequest)WebRequest.Create("https://XXXX.XXXX.XXXX/api/v2/EEEE/login");
        login l = new login();
        l.user = "XXX";
        l.password = "XXXX@123";
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(l);
        byte[] byteArray = Encoding.UTF8.GetBytes(json);
        request.Accept = "application/json";
        request.Method = "POST";
       // request.Host = "XXXX.XXXX.com";
      //  request.ContentType = "application/json";
        //request.Headers.Add("Host", "XXX.XXX.XXX);
        request.Headers.Add("BSN-XXXX", "XXXXX");
        request.Headers.Add("BSN-XXXX", "XXXXXXXX");
        request.ContentLength = byteArray.Length;
        //request.Referer = "XXXXXX.XXXX.COM";
        using (var stream = request.GetRequestStream())
        {
            stream.Write(byteArray, 0, byteArray.Length);
        }


         //request.KeepAlive = false;
        //    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | //SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    }

i used this code and it works fine我使用了这段代码,它工作正常

 var client = new RestClient("https://XXXX.XXXXXX/api/v2/jumana/login/");
           // client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("XXX", "XXXX");
            request.AddHeader("XXXX", "XXXXX");
            request.AddParameter("application/json", "{\n\"user\":\"ZZZZZZ\",\n\"password\":\"CUXXXBXZAE@XXX\"\n}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM