HTTP Post VB.net 中的请求
HTTP Post Request in VB.net
我一直在尝试从 visual basic 发出 Http post 请求,但每次我收到异常时,身份验证或 headers 都没有问题,因为相同的代码在 C# 中有效,我在 VB.Net 中缺少做什么才能让它发挥作用?
这是我尝试过的:
[vb]
Sub test()
Dim URL As String = "https://myurl.com.do"
Dim User As String = "user"
Dim Key As String = "key"
Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri(URL)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("Auth1", User)
client.DefaultRequestHeaders.Add("Auth2", Key)
client.Timeout = New TimeSpan(0, 0, 160)
Dim values = New Dictionary(Of String, String) From {
{"param1", "1.00"},
{"param2", "0"},
{"param3", "1.00"},
{"param4", "349000000"}
}
Dim content = New FormUrlEncodedContent(values)
Try
Dim response = client.PostAsync(URL, content).Result
If response.IsSuccessStatusCode Then
Try
Dim OB As Object = JsonConvert.DeserializeObject(Of Object)(response.Content.ReadAsStringAsync().Result)
Catch ex As Exception
Console.WriteLine("error")
End Try
Else
Console.WriteLine("{0} ({1})", CInt(response.StatusCode), response.ReasonPhrase)
End If
Console.ReadKey()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
这是我遇到的异常:
(第一次尝试捕获异常,在 Dim 响应行上):
这是我的工作 C# 代码:
static void Main(string[] args)
{
string URL = "https://myurl.com.do";
String User = "user";
String Key = "key";
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("param1", "1.00");
values.Add("param2", "0");
values.Add("param3", "1.00");
values.Add("param4", "349000000");
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.Timeout = new TimeSpan(0, 0, 160);
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") );
client.DefaultRequestHeaders.Add("Auth1", User);
client.DefaultRequestHeaders.Add("Auth2", Key);
try
{
HttpResponseMessage response = client.PostAsync(URL, content).Result;
if (response.IsSuccessStatusCode)
{
try
{
string b = response.Content.ReadAsStringAsync().Result;
Dictionary<string, string> dataObjects = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);// response.Content.read
object OB = JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result);// response.Content.read
}
catch (Exception)
{
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error de conexion");
}
}
我在 vb.net 中遗漏了什么或做错了什么?
从下一个link复制粘贴的答案:
我认为这是因为您正在连接到“https”url。在这种情况下,您必须在代码中添加以下行。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
它将接受您的请求的“ssl”协议。 ServicePointManager.ServerCertificateValidationCallback
处理程序只控制证书有效性。
我一直在尝试从 visual basic 发出 Http post 请求,但每次我收到异常时,身份验证或 headers 都没有问题,因为相同的代码在 C# 中有效,我在 VB.Net 中缺少做什么才能让它发挥作用?
这是我尝试过的: [vb]
Sub test()
Dim URL As String = "https://myurl.com.do"
Dim User As String = "user"
Dim Key As String = "key"
Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri(URL)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("Auth1", User)
client.DefaultRequestHeaders.Add("Auth2", Key)
client.Timeout = New TimeSpan(0, 0, 160)
Dim values = New Dictionary(Of String, String) From {
{"param1", "1.00"},
{"param2", "0"},
{"param3", "1.00"},
{"param4", "349000000"}
}
Dim content = New FormUrlEncodedContent(values)
Try
Dim response = client.PostAsync(URL, content).Result
If response.IsSuccessStatusCode Then
Try
Dim OB As Object = JsonConvert.DeserializeObject(Of Object)(response.Content.ReadAsStringAsync().Result)
Catch ex As Exception
Console.WriteLine("error")
End Try
Else
Console.WriteLine("{0} ({1})", CInt(response.StatusCode), response.ReasonPhrase)
End If
Console.ReadKey()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
这是我遇到的异常: (第一次尝试捕获异常,在 Dim 响应行上):
这是我的工作 C# 代码:
static void Main(string[] args)
{
string URL = "https://myurl.com.do";
String User = "user";
String Key = "key";
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("param1", "1.00");
values.Add("param2", "0");
values.Add("param3", "1.00");
values.Add("param4", "349000000");
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.Timeout = new TimeSpan(0, 0, 160);
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") );
client.DefaultRequestHeaders.Add("Auth1", User);
client.DefaultRequestHeaders.Add("Auth2", Key);
try
{
HttpResponseMessage response = client.PostAsync(URL, content).Result;
if (response.IsSuccessStatusCode)
{
try
{
string b = response.Content.ReadAsStringAsync().Result;
Dictionary<string, string> dataObjects = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);// response.Content.read
object OB = JsonConvert.DeserializeObject<object>(response.Content.ReadAsStringAsync().Result);// response.Content.read
}
catch (Exception)
{
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error de conexion");
}
}
我在 vb.net 中遗漏了什么或做错了什么?
从下一个link复制粘贴的答案:
我认为这是因为您正在连接到“https”url。在这种情况下,您必须在代码中添加以下行。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
它将接受您的请求的“ssl”协议。 ServicePointManager.ServerCertificateValidationCallback
处理程序只控制证书有效性。