VB.net中的HTTP发布请求

时间:2018-11-15 14:11:35

标签: c# vb.net http post http-post

我一直试图从Visual Basic中发出Http post请求,但是每次遇到异常时,身份验证或标头都没有问题,因为相同的代码在C#中有效,所以我在VB中缺少什么。可以使网络正常工作吗?

这是我尝试过的: [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响应行上):

exception on first try catch

Inner exeption

这是我正在使用的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中缺少什么或做错了什么?

1 个答案:

答案 0 :(得分:1)

从下一个链接中复制粘贴的答案:

Http post error: An existing connection was forcibly closed by the remote host

我认为这是因为您要连接到“ https” URL。在这种情况下,您必须在代码中添加以下行。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

它将接受您的请求的“ ssl”协议。 “ ServicePointManager.ServerCertificateValidationCallback”处理程序仅控制证书的有效性。