※肝は漢字のデータ ずっとこれが原因でエラー(400)がでていた
- JOSNデータをシリアライズ化
- その結果をエンコード
- 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