HTTP Post Request in VB.net [duplicate]
This question already has an answer here:
Http post error: An existing connection was forcibly closed by the remote host
2 answers
I've been trying to make an Http post request from visual basic but every time I get an exception, there is nothing wrong with authentication or headers since the same code works in C#, what am I missing to do in VB.Net to make it work?
here is what I've tried:
[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
and this is the exception I'm getting:
(exception on first try catch,on the Dim response line ):
this is my working C# code:
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");
}
}
what am I missing or doing wrong in vb.net?
c# vb.net http post http-post
marked as duplicate by the_lotus, Community♦ Nov 15 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Http post error: An existing connection was forcibly closed by the remote host
2 answers
I've been trying to make an Http post request from visual basic but every time I get an exception, there is nothing wrong with authentication or headers since the same code works in C#, what am I missing to do in VB.Net to make it work?
here is what I've tried:
[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
and this is the exception I'm getting:
(exception on first try catch,on the Dim response line ):
this is my working C# code:
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");
}
}
what am I missing or doing wrong in vb.net?
c# vb.net http post http-post
marked as duplicate by the_lotus, Community♦ Nov 15 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
1
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33
add a comment |
This question already has an answer here:
Http post error: An existing connection was forcibly closed by the remote host
2 answers
I've been trying to make an Http post request from visual basic but every time I get an exception, there is nothing wrong with authentication or headers since the same code works in C#, what am I missing to do in VB.Net to make it work?
here is what I've tried:
[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
and this is the exception I'm getting:
(exception on first try catch,on the Dim response line ):
this is my working C# code:
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");
}
}
what am I missing or doing wrong in vb.net?
c# vb.net http post http-post
This question already has an answer here:
Http post error: An existing connection was forcibly closed by the remote host
2 answers
I've been trying to make an Http post request from visual basic but every time I get an exception, there is nothing wrong with authentication or headers since the same code works in C#, what am I missing to do in VB.Net to make it work?
here is what I've tried:
[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
and this is the exception I'm getting:
(exception on first try catch,on the Dim response line ):
this is my working C# code:
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");
}
}
what am I missing or doing wrong in vb.net?
This question already has an answer here:
Http post error: An existing connection was forcibly closed by the remote host
2 answers
c# vb.net http post http-post
c# vb.net http post http-post
edited Nov 15 '18 at 14:22
Misael Moneró Thompson
asked Nov 15 '18 at 14:11
Misael Moneró ThompsonMisael Moneró Thompson
316311
316311
marked as duplicate by the_lotus, Community♦ Nov 15 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by the_lotus, Community♦ Nov 15 '18 at 18:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
1
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33
add a comment |
1
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
1
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33
1
1
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
1
1
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
copy pasted answer from this next link:
Http post error: An existing connection was forcibly closed by the remote host
I think its because you are connecting to "https" url. In this case you have to add following line to your code.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
copy pasted answer from this next link:
Http post error: An existing connection was forcibly closed by the remote host
I think its because you are connecting to "https" url. In this case you have to add following line to your code.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.
add a comment |
copy pasted answer from this next link:
Http post error: An existing connection was forcibly closed by the remote host
I think its because you are connecting to "https" url. In this case you have to add following line to your code.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.
add a comment |
copy pasted answer from this next link:
Http post error: An existing connection was forcibly closed by the remote host
I think its because you are connecting to "https" url. In this case you have to add following line to your code.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.
copy pasted answer from this next link:
Http post error: An existing connection was forcibly closed by the remote host
I think its because you are connecting to "https" url. In this case you have to add following line to your code.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It will accept "ssl" protocol for your request. "ServicePointManager.ServerCertificateValidationCallback" handler just controls certificate validity.
answered Nov 15 '18 at 14:35
Misael Moneró ThompsonMisael Moneró Thompson
316311
316311
add a comment |
add a comment |
1
I can't see images. Can you put the text of the exception?
– the_lotus
Nov 15 '18 at 14:24
1
Is there a reason for not typing 'response'? Do you have Option Infer On? You're also dropping explicit typing on other variables - these are 'Object' if you don't have Option Infer On.
– Dave Doknjas
Nov 15 '18 at 14:27
Thank you all guys! just digging into the inner exeptions found the real issue, the answer is in this link: stackoverflow.com/questions/28453353/…
– Misael Moneró Thompson
Nov 15 '18 at 14:33