1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vb.net(2010)でリクエスト(JSON)を送信する

Last updated at Posted at 2019-02-08

※肝は漢字のデータ  ずっとこれが原因でエラー(400)がでていた

  1. JOSNデータをシリアライズ化
  2. その結果をエンコード
  3. WebClientのUploadDataを使ってJSONをリクエスト送信する

#リクエスト側(クライアント)


	Imports System.ServiceModel.Channels
	Imports System.Net
	Imports System.Collections.Generic
	Imports System.Runtime.Serialization.Json
	Imports System.Runtime.InteropServices
	Imports System.Security.Cryptography.X509Certificates
	Imports System.Text
	Imports System.IO
	Imports System.ServiceModel.Web
	Private Function RequestToPz3(ByRef json As RequestJson3) As String
        '--- 文字コードを指定する
        Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("UTF-8")
        '--- WebClientクラスの定義
        Dim wc As New System.Net.WebClient
        '--- ヘッダ(Content-Type)
        wc.Headers.Add("Content-Type", "application/json; charset=utf-8")
        Dim eMsg As String = ""
        Try
            Dim result As String
            Using stream As New IO.MemoryStream
                '---    シリアライズ実行
                Dim serializer As New DataContractJsonSerializer(json.GetType)
                serializer.WriteObject(stream, json)
                '---    結果を取得
                result = System.Text.Encoding.UTF8.GetString(stream.ToArray())
            End Using
            '*****  データを送信し、結果を受信する *****
            reqCount = reqCount + 1
            Dim resData As Byte()
            Try
                '---    これで漢字も送れた
                Dim postDataBytes2 As Byte() = enc.GetBytes(result)
                resData = wc.UploadData(tmpRequestUrl, postDataBytes2)      '成功
            Catch ex As Exception
            End Try
            wc.Dispose()
            '受信した結果(JSONデータ)を表示する
            Dim resText As String = enc.GetString(resData)
            Console.WriteLine(resText)
        Catch e As WebException
            ' 予期せぬエラー
        Catch e As System.Exception
        End Try
        Return eMsg
    End Function

#受取側(コントローラ)


	Option Strict On
	Imports System.ServiceModel
	Imports System.ServiceModel.Activation
	Imports System.ServiceModel.Web
	Imports System.Runtime.Serialization
	Imports System.Collections.Generic
	Imports System.Net
	Imports System.ServiceModel.Channels
	Imports System.Text

	'*******************************************************************************************************
	'   処理概要:コントローラー
	'*******************************************************************************************************
	<ServiceContract(namespace:="http://aaaaaaa/bbb/api/assets/")>
	<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
	Public Class ss09030r

	    Inherits System.Web.UI.Page
	    Private Const X_REQUEST_ID = "X-request-id"
	    '--------------------------------------------------------------------
	    '   reportコントローラー	ここでリクエストを受取る
	    '--------------------------------------------------------------------
	    <OperationContract(), WebInvoke()>
	    Public Function report(req As RequestJson3) As RequestJson3
	        Dim RequestHeaders = WebOperationContext.Current.IncomingRequest.Headers        '   リクエストヘッダー
	        Return req
	    End Function
	End Class
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?