2012-02-28 24 views
6

クラスライブラリのサービス参照に3つのWebサービスが追加されています(これはAPIを使用するサンプルプロジェクトです)私のプロジェクトに移動する必要がありますが、セキュリティ上の問題セキュリティ上の問題は、サービスが1つのIPアドレスにしか応答せず、それが顧客のサーバーのIPアドレスであることを意味します。)そのパーシャルカルウェブサービスに「Ildasm.exe」を使用するようなクラスを生成する方法はありますか?参照を追加しないWebサービス?

+0

サービスが特定のIPアドレスにしか応答しない場合、どのようにコードをテストする予定ですか?私の提案は、サービスのWSDLを所有者から取得することであり、svcutilを実行してそれと対話するクラスを生成できるはずです。 – Rich

答えて

8

は、Webサービスのコードを再生するには、Webサービス参照を追加する必要はありません:あなたは、手動などで再生するには、クラスを生成することができます。

wsdl.exe /out:d:/Proxy.cs /順序をhttp://localhost:2178/Services.asmx

このファイルをプロジェクトに手動で追加することができます。

+1

私をビートしてください:)参考のために - > http://msdn.microsoft.com/en-us/library/ms155134.aspx – SeanCocteau

+0

私はsvcutil.exeを調べることをお勧めします –

32

このクラスを使用できます。私は基本的なコードが見つかった場所を覚えていない、私はいくつかのメソッドを追加し、前にクラスに変換します。

public class WebService 
{ 
    public string Url { get; set; } 
    public string MethodName { get; set; } 
    public Dictionary<string, string> Params = new Dictionary<string, string>(); 
    public XDocument ResultXML; 
    public string ResultString; 

    public WebService() 
    { 

    } 

    public WebService(string url, string methodName) 
    { 
     Url = url; 
     MethodName = methodName; 
    } 

    /// <summary> 
    /// Invokes service 
    /// </summary> 
    public void Invoke() 
    { 
     Invoke(true); 
    } 

    /// <summary> 
    /// Invokes service 
    /// </summary> 
    /// <param name="encode">Added parameters will encode? (default: true)</param> 
    public void Invoke(bool encode) 
    { 
     string soapStr = 
      @"<?xml version=""1.0"" encoding=""utf-8""?> 
      <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
       xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
       xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> 
       <soap:Body> 
       <{0} xmlns=""http://tempuri.org/""> 
        {1} 
       </{0}> 
       </soap:Body> 
      </soap:Envelope>"; 

     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url); 
     req.Headers.Add("SOAPAction", "\"http://tempuri.org/" + MethodName + "\""); 
     req.ContentType = "text/xml;charset=\"utf-8\""; 
     req.Accept = "text/xml"; 
     req.Method = "POST"; 

     using (Stream stm = req.GetRequestStream()) 
     { 
      string postValues = ""; 
      foreach (var param in Params) 
      { 
       if (encode) 
        postValues += string.Format("<{0}>{1}</{0}>", HttpUtility.UrlEncode(param.Key), HttpUtility.UrlEncode(param.Value)); 
       else 
        postValues += string.Format("<{0}>{1}</{0}>", param.Key, param.Value); 
      } 

      soapStr = string.Format(soapStr, MethodName, postValues); 
      using (StreamWriter stmw = new StreamWriter(stm)) 
      { 
       stmw.Write(soapStr); 
      } 
     } 

     using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream())) 
     { 
      string result = responseReader.ReadToEnd(); 
      ResultXML = XDocument.Parse(result); 
      ResultString = result; 
     } 
    } 
} 

そして、あなたはこの

WebService ws = new WebService("service_url", "method_name"); 
ws.Params.Add("param1", "value_1"); 
ws.Params.Add("param2", "value_2"); 
ws.Invoke(); 
// you can get result ws.ResultXML or ws.ResultString 
+0

これを使用するには、Web APIメソッドを呼び出すことは可能ですか?メソッドを呼び出す? – altandogan

+0

http://stackoverflow.com/questions/40394297/consume-web-service-dynamically-using-httpwebrequest-with-service-referenceでご意見をお寄せください –

0

のように使用することができますあなたが参照wsdl.exe /out:d:/Proxy.cs /orderを追加したくない場合は、[はい代替

2

だろうあなたが動的にサービス参照のURLを変更することができます。

var service = new MyService.MyWSSoapClient(); 
service.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:8080/"); 
2

ここでは、C#コードから「GET」Webサービスを呼び出す方法の例を示します:

public string CallWebService(string URL) 
{ 
    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL); 
    objRequest.Method = "GET"; 
    objRequest.KeepAlive = false; 

    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); 
    string result = ""; 
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) 
    { 
     result = sr.ReadToEnd(); 
     sr.Close(); 
    } 
    return result; 
} 

単純にURLを渡すと、応答を含む文字列が返されます。そこから、あなたが何か役に立つに文字列を有効にするJSON.Net「DeserializeObject」関数を呼び出すことができます。このことができます

string JSONresponse = CallWebService("http://www.inorthwind.com/Service1.svc/getAllCustomers"); 

List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(JSONresponse); 

希望。

関連する問題