2012-02-28 116 views
6

我有3個Web服務添加到類庫中的服務引用(這是API使用的示例項目)我需要將這些內容移動到我的項目,但由於安全問題,我無法添加服務引用(通過安全問題我的意思是服務只響應一個IP地址,這是我們客戶的服務器的IP地址。)有沒有辦法生成一個類,如使用「Ildasm.exe」該particaluar網絡服務?沒有添加引用的Web服務?

+0

如果服務只響應某些IP地址,您如何計劃測試您的代碼?我的建議是從它的擁有者那裏獲得服務的WSDL,並且你應該能夠運行svcutil來生成與它交互的類。 – Rich 2012-02-28 22:50:51

回答

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 2012-02-28 13:45:38

+0

我建議看看svcutil.exe – 2012-02-28 14:03:29

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 2015-10-13 20:21:43

+0

請分享您的想法http://stackoverflow.com/questions/40394297/consume-web-service-dynamically-using-httpwebrequest-without-service-reference – 2016-11-03 06:43:36

0

是,如果你不想加入參考wsdl.exe /out:d:/Proxy.cs /order將替代

2

可以動態改變一個服務引用的網址:

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); 

希望這有助於。

相關問題