Visual Basic 6调用.NET Core API POST请求报415错误求助
VB6调用.NET Core API POST请求返回415错误的解决方法
415错误表示服务器不支持请求发送的媒体类型,问题出在两个关键地方:
请求头名称错误:你设置的
ContentType不是标准HTTP请求头,正确的头名是Content-Type(中间必须包含连字符)。修改这行代码:theHTTP.setRequestHeader "Content-Type", "application/json"JSON格式不规范:你的请求体不符合JSON标准规范——JSON要求键名和字符串值必须使用双引号,不能用单引号。同时VB6中双引号需要用两个双引号转义,修正后的请求体如下:
theBody = "{""keyvault"": { ""URI"": ""bla bla bla"", ""secretname"": ""the user id""}}"可选优化:指定字符编码:如果API对字符编码有要求,可以在
Content-Type中补充编码信息,避免编码不匹配问题:theHTTP.setRequestHeader "Content-Type", "application/json; charset=utf-8"
修复后的完整POST请求代码:
Dim theURL As String Dim theHTTP As Object Dim theBody As String Dim theResponse As String theURL = "https://somewhere" theBody = "{""keyvault"": { ""URI"": ""bla bla bla"", ""secretname"": ""the user id""}}" Set theHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0") theHTTP.Open "POST", theURL, False theHTTP.setRequestHeader "Content-Type", "application/json; charset=utf-8" theHTTP.Send theBody theResponse = theHTTP.ResponseText
内容的提问来源于stack exchange,提问作者Marianne Seggerman
相关产品推荐
相关产品推荐

