[英]400 Bad Request for WebRequest over https
When I make a request to the endpoint https://api.mymoney.co.zw/api/v1/addcart in Visual Studio Code 2017 I am getting an exception: The remote server returned an error: (400) Bad Request.当我在 Visual Studio Code 2017 中向端点https://api.mymoney.co.zw/api/v1/addcart发出请求时,出现异常:远程服务器返回错误:(400)错误请求。 .
. This is only happening within context of VS2017.
这仅发生在 VS2017 的上下文中。
My code:我的代码:
public static void Main(string[] args)
{
GetStatistics();
}
static async Task<List<Root2>> GetStatistics()
{
List<Root2> details = GetRemmitances();
List<Root2> values2 = details.Take(1).ToList();
string responsePath = @"C:\ABC\";
string responseLog = "ABC_Response";
var responsePathabs = responsePath + Convert.ToString(responseLog) + DateTime.Today.ToString("dd -MM-yy") + ".txt";
// ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.mymoney.co.zw/api/v1/addcart");
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("x-api-key", Helpers.apikey.ToString());
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
try
{
foreach (var tran in values2)
{
var lis = JsonConvert.SerializeObject(tran);
streamWriter.Write(tran);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
resuit = streamReader.ReadToEnd();
if (!File.Exists(responsePathabs))
{
File.Create(responsePathabs).Dispose();
}
using (StreamWriter sw = File.AppendText(responsePathabs))
{
sw.WriteLine(resuit);
sw.Flush();
sw.Close();
}
}
}
}
catch (WebException ex)
{
ExceptionLogger.SendErrorToText(ex);
}
} }
I have put a breakpoint and copied the payload in lis to Postman - it executes successfully with no issues.我已经放置了一个断点并将lis中的有效负载复制到 Postman - 它成功执行,没有任何问题。
What am I missing?我错过了什么?
Is this something particular to the context of visual studio or?这是视觉工作室环境中特有的东西还是? I have tried some suggestions ie to place ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 |
我尝试了一些建议,即放置ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12 before instantatuion of the WebRequest but its still yielding same error.
System.Net.SecurityProtocolType.Tls12在 WebRequest 实例化之前,但它仍然产生相同的错误。
My code is a Console Program running off a corporate network - all proxies however are disabled for the call.我的代码是一个在公司网络上运行的控制台程序 - 但是所有代理都被禁用了调用。
You are writing tran
to the stream instead of the serialized JSON data in lis
.您正在将
tran
写入 stream 而不是lis
中的序列化 JSON 数据。
var lis = JsonConvert.SerializeObject(tran);
streamWriter.Write(tran); // <- should be lis instead of tran
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.