Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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"
  }
}

リソース

https://miso-soup3.hateblo.jp/entry/2014/06/02/000603

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away