Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,
I am new to web services, I have a task to send the JSON content to particular URL using post method. relevant JSON and tried code available below, I have faced the exception only. anyone have a better solution for this.

You can provide c# solution also, I will convert in my end. thank you

What I have tried:

VB
''My.Settings.JsonViewerURLhttp://176.26.181.67/ProcessTool/api/processBook
Using clnt As WebClient = New WebClient()
            clnt.Headers.Add("Content-Type:application/json")
            clnt.Headers.Add("Accept:application/json")
            Dim res As VariantType = clnt.UploadString(My.Settings.JsonViewerURL, JsonConvert.SerializeObject(jsonvalue))
            Console.WriteLine(res)
        End Using
        Exit Sub


''Json Content
''{"ToolName":"ProcessTool","Version":"3.3","TimeStamp":"2020-04-10 19:16:26","UserName":"User1","BookNumber":"485_1_01","Machine":"CHN-152","StartTime":"19-15-29","EndTime":"19-16-26","Meta":"ProcessMeta.xml","InputFile":[{"FileName":"InfileForTest.docx","Format":".docx","PageCount":"214"}],"OutputFile":[{"FileName":"485_1_FM.docx","Component":"FM","Part":"FM","Title":"","Number":"","Format":".docx","PageCount":"18"},{"FileName":"485_1_CH1.docx","Component":"CHAPTER","Part":"","Title":"Ch1Title","Number":"1","Format":".docx","PageCount":"33"},{"FileName":"485_1_CH2.docx","Component":"CHAPTER","Part":"","Title":"Ch2Title","Number":"2","Format":".docx","PageCount":"15"},{"FileName":"485_1_CH3.docx","Component":"CHAPTER","Part":"","Title":"Ch3 Title","Number":"3","Format":".docx","PageCount":"24"},{"FileName":"485_1_CH4.docx","Component":"CHAPTER","Part":"","Title":"Ch4Title","Number":"4","Format":".docx","PageCount":"42"}]}
Posted
Updated 18-Apr-22 12:50pm
v2
Comments
MadMyche 11-Apr-20 10:27am    
It would b helpful to now what Exception you are seeing

 
Share this answer
 
For the purpose of simplicity and ease, here is the code from the link: JSON HTTP POST Request In Visual Basic .NET · GitHub[^]

CODE

'Install Newtonsoft.json
'-----------------------
'
'PM> Install-Package Newtonsoft.Json -Version 6.0.8

'Sample Usage
'------------

'Dim jsonPost As New JsonPost("http://192.168.254.104:8000")
'Dim dictData As New Dictionary(Of String, Object)
'dictData.Add("test_key", "test_value")
'jsonPost.postData(dictData)

Imports Newtonsoft.Json
Imports System.Net
Imports System.Text
Public Class JsonPost

    Private urlToPost As String = ""

    Public Sub New(ByVal urlToPost As String)
        Me.urlToPost = urlToPost
    End Sub

    Public Function postData(ByVal dictData As Dictionary(Of String, Object)) As Boolean
        Dim webClient As New WebClient()
        Dim resByte As Byte()
        Dim resString As String
        Dim reqString() As Byte

        Try
            webClient.Headers("content-type") = "application/json"
            reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dictData, Formatting.Indented))
            resByte = webClient.UploadData(Me.urlToPost, "post", reqString)
            resString = Encoding.Default.GetString(resByte)
            Console.WriteLine(resString)
            webClient.Dispose()
            Return True
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Return False
    End Function

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900