Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VB.net中的HTTP Post请求_C#_Vb.net_Http_Post_Http Post - Fatal编程技术网

C# VB.net中的HTTP Post请求

C# VB.net中的HTTP Post请求,c#,vb.net,http,post,http-post,C#,Vb.net,Http,Post,Http Post,我一直在尝试从VisualBasic发出Http post请求,但每次我遇到异常时,身份验证或头都没有问题,因为相同的代码在C#中工作,我缺少在VB.Net中做什么才能使其工作 以下是我尝试过的: [vb] 这是我得到的一个例外: (第一次尝试捕捉时,Dim响应线上出现异常): 这是我的工作C#代码: static void Main(字符串[]args) { 字符串URL=”https://myurl.com.do"; 字符串User=“User”; 字符串Key=“Ke

我一直在尝试从VisualBasic发出Http post请求,但每次我遇到异常时,身份验证或头都没有问题,因为相同的代码在C#中工作,我缺少在VB.Net中做什么才能使其工作

以下是我尝试过的: [vb]

这是我得到的一个例外: (第一次尝试捕捉时,Dim响应线上出现异常):

这是我的工作C#代码:

static void Main(字符串[]args)
{
字符串URL=”https://myurl.com.do";         
字符串User=“User”;
字符串Key=“Key”;
字典值=新字典();
添加(“参数1”、“1.00”);
添加(“参数2”、“0”);
添加(“参数3”、“1.00”);
增加(“参数4”、“349000000”);
FormUrlEncodedContent内容=新的FormUrlEncodedContent(值);
HttpClient=新的HttpClient();
client.BaseAddress=新Uri(URL);
client.Timeout=新的时间跨度(0,0,160);
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.DefaultRequestHeaders.Add(“Auth1”,用户);
client.DefaultRequestHeaders.Add(“Auth2”,Key);
尝试
{
HttpResponseMessage response=client.PostAsync(URL、内容).Result;
if(响应。IsSuccessStatusCode)
{                   
尝试
{
字符串b=response.Content.ReadAsStringAsync().Result;
Dictionary dataObjects=JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);//response.Content.read
object OB=JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);//response.Content.read
}
捕获(例外)
{
}
}
其他的
{
Console.WriteLine(“{0}({1})”,(int)response.StatusCode,response.ReasonPhrase);
}
Console.ReadKey();
}
捕获(例外情况除外)
{
Console.WriteLine(“连接错误”);
}
}

我在vb.net中遗漏了什么或做错了什么?

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

我想这是因为你正在连接“https”url。在这种情况下,您必须在代码中添加以下行

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

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

我看不到图像。你能输入异常的文本吗?有没有不输入“response”的原因?你有选择推断吗?您还删除了对其他变量的显式键入-如果您没有选项推断,这些变量就是“对象”。谢谢大家!只要深入挖掘内部的例外就可以发现真正的问题,答案就在这个链接中:
 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");
        }
    }
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;