Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''Code to Send Http JSON POST request
- ''Get the response from the server
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Text
- Imports System.Net
- Imports System.IO
- Module Module1
- Sub Main()
- Dim JsonData As String = "{""codigoEstabelecimento"":""1000000000000"",""codigoFormaPagamento"":""170"",""transacao"":{""numeroTransacao"":""4"",""valor"":""100"",""valorDesconto"":""0"",""parcelas"":""1"",""urlCampainha"":"",""urlResultado"":""/retorno.php"",""ip"":""127.0.0.1"",""idioma"":""1"",""campoLivre1"":"",""campoLivre2"":"",""campoLivre3"":"",""campoLivre4"":"",""campoLivre5"":"",""dataVencimentoBoleto"":""},""dadosCartao"":{""nomePortador"":""Teste Teste"",""numeroCartao"":""4111111111111111"",""codigoSeguranca"":""125"",""dataValidade"":""11/2019""},""itensDoPedido"":[{""codigoProduto"":""1"",""nomeProduto"":""Produto 1"",""codigoCategoria"":""1"",""nomeCategoria"":""Categoria"",""quantidadeProduto"":""1"",""valorUnitarioProduto"":""100""}],""dadosCobranca"":{""codigoCliente"":""1"",""tipoCliente"":""1"",""nome"":""Cliente Teste"",""email"":""[email protected]"",""dataNascimento"":"""",""sexo"":""F"",""documento"":"",""documento2"":"""",""endereco"":[{""logradouro"":""Rua"",""numero"":""123"",""complemento"":""Casa 1"",""cep"":""17516000"",""bairro"":""Bairro"",""cidade"":""Marilia"",""estado"":""SP"",""pais"":""BR""}],""telefone"":[{""tipoTelefone"":""1"",""ddi"":""55"",""ddd"":""14"",""telefone"":""33011122""}] }}"
- Dim myRequest As HttpWebRequest = PostJSON(JsonData)
- Console.WriteLine("Response of Request:{0}", GetResponse(myRequest))
- Console.ReadKey()
- End Sub
- Private Function PostJSON(ByVal JsonData As String) As HttpWebRequest
- Dim objhttpWebRequest As HttpWebRequest
- Try
- Dim httpWebRequest = DirectCast(WebRequest.Create("URL DO GATEWAY"), HttpWebRequest)
- httpWebRequest.ContentType = "text/json"
- httpWebRequest.Method = "POST"
- Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
- streamWriter.Write(JsonData)
- streamWriter.Flush()
- streamWriter.Close()
- End Using
- objhttpWebRequest = httpWebRequest
- Catch ex As Exception
- Console.WriteLine("Send Request Error[{0}]", ex.Message)
- Return Nothing
- End Try
- Return objhttpWebRequest
- End Function
- Private Function GetResponse(ByVal httpWebRequest As HttpWebRequest) As String
- Dim strResponse As String = "Bad Request:400"
- Try
- Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
- Using streamReader = New StreamReader(httpResponse.GetResponseStream())
- Dim result = streamReader.ReadToEnd()
- strResponse = result.ToString()
- End Using
- Catch ex As Exception
- Console.WriteLine("GetResponse Error[{0}]", ex.Message)
- Return ex.Message
- End Try
- Return strResponse
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement