0
0

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 5 years have passed since last update.

Webサービス(ASMX)のSOAPからJSONに変換

Last updated at Posted at 2019-10-27

TL;DR

  • Webサービス(ASMX)のSOAPからJSONに変換
  • 既存のWebサービス(ASMX)のSOAPを変更せず、JSONに変換して使用したい場合に使用する

できていること

  • GETでWebサービス(ASMX)のメソッドを呼び出す
    • 呼び出し元のweb.configでGETを許可する設定<add name="HttpGet"/>が必要
  • Webサービス(ASMX)のメソッドからSOAPを取得してJSONに変換

まだできていないこと

  • POSTで渡したJSONから値を取り出す
  • 取り出した値からPOSTでWebサービス(ASMX)のメソッドを呼び出す

ソース

SOAPのサーバー

web.config
<configuration>
  <system.web><webServices>
      <protocols>
        <add name="HttpSoap"/>
        <add name="HttpGet"/>
      </protocols>
    </webServices>
  </system.web></configuration>
WebService1.asmx.vb
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System.Web.Script.Services

<WebService(Namespace:="http://tempuri.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()>
    Public Function FuncXml(userid As String) As XmlDocument
        Dim doc As New XmlDocument

        Dim eRoot = doc.CreateElement("ROOTXML")
        doc.AppendChild(eRoot)

        Dim eC1 = doc.CreateElement("C1")
        eRoot.AppendChild(eC1)

        Dim n1 = doc.CreateNode(XmlNodeType.Text, "", "")
        n1.Value = userid
        eC1.AppendChild(n1)

        Return doc
    End Function

End Class

SOAP→JSONのサーバー

Convert.asmx.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Xml.Linq;

namespace SOAP2JSON
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Convert : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public void Soap2Json(string targetUri)
        {
            var xdoc = GetXmlDocument(targetUri);

            Context.Response.Clear();
            Context.Response.ContentType = "application/json";

            var json = JsonConvert.SerializeXNode(xdoc, Newtonsoft.Json.Formatting.Indented);
            Context.Response.Write(json);
        }

        private XDocument GetXmlDocument(string uri)
        {
            var request = WebRequest.Create(uri);
            request.Credentials = CredentialCache.DefaultCredentials;

            XDocument xdoc;
            using (var response = request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    xdoc = XDocument.Load(dataStream);
                }
            }
            return xdoc;
        }
    }
}

結果

変換前

<?xml version="1.0" encoding="UTF-8"?>
<ROOTXML>
<C1>9876</C1>
</ROOTXML>

変換後

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
  },
  "ROOTXML": {
    "C1": "9876"
  }
}

リソース

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?