You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET发送CURL POST请求遇400错误,寻求技术帮助

排查VB.NET调用name.com域名创建API返回400 Bad Request的问题

关键排查步骤

1. 捕获并查看400错误的详细响应

400错误的核心是服务器明确拒绝了请求,但你的代码未捕获异常并读取错误响应。添加异常处理可以获取服务器给出的具体错误原因:

Try
    myResp = myReq.GetResponse()
    Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
    WebResponse = myreader.ReadToEnd()
    MsgBox(WebResponse)
Catch ex As WebException
    If ex.Response IsNot Nothing Then
        Using errorResponse As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
            Using reader As New StreamReader(errorResponse.GetResponseStream())
                Dim errorText = reader.ReadToEnd()
                MsgBox($"错误详情:{errorText}") ' 这里会显示服务器返回的具体错误,比如参数缺失、格式错误等
            End Using
        End Using
    End If
End Try

2. 补充Content-Length请求头

在写入请求流前,添加Content-Length头,确保服务器能正确识别请求体大小:

dataByte = Encoding.UTF8.GetBytes(data)
request.ContentLength = dataByte.Length ' 新增这一行
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)

3. 避免手动拼接JSON,用序列化生成

手动转义JSON引号容易出错,使用Newtonsoft.Json(NuGet安装)来序列化对象生成JSON:

' 先定义对应的数据类
Public Class DomainRequest
    Public Property domain As DomainInfo
    Public Property purchasePrice As Decimal
End Class

Public Class DomainInfo
    Public Property domainName As String
End Class

' 生成JSON的代码
Dim requestObj As New DomainRequest With {
    .domain = New DomainInfo With {.domainName = "example.org"},
    .purchasePrice = 12.99D
}
Dim data As String = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj)

4. 改用HttpClient(推荐方案)

HttpClient是.NET中更现代的HTTP客户端,自动处理Content-Length、请求头等,代码更简洁:

Dim httpClient As New HttpClient()
Dim credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{txtUsername.Text.Trim}:{txtApiKey.Text.Trim}"))
httpClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")

Dim requestObj As New DomainRequest With {
    .domain = New DomainInfo With {.domainName = "example.org"},
    .purchasePrice = 12.99D
}
Dim jsonContent = New StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestObj), Encoding.UTF8, "application/json")

Try
    Dim response = Await httpClient.PostAsync("https://api.dev.name.com/v4/domains", jsonContent)
    response.EnsureSuccessStatusCode() ' 非200状态会抛出异常
    Dim responseText = Await response.Content.ReadAsStringAsync()
    MsgBox(responseText)
Catch ex As HttpRequestException
    If ex.Response IsNot Nothing Then
        Dim errorText = Await ex.Response.Content.ReadAsStringAsync()
        MsgBox($"错误详情:{errorText}")
    End If
End Try

常见的具体错误原因

根据name.com的API文档,400错误常见原因包括:

  • 域名已被注册或无法购买
  • purchasePrice与实际可购买价格不匹配
  • 授权信息错误(用户名/API密钥无效)
  • JSON格式错误(比如引号转义错误、字段名拼写错误)

内容的提问来源于stack exchange,提问作者digitaldrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:29:54