我有3個Web服務添加到類庫中的服務引用(這是API使用的示例項目)我需要將這些內容移動到我的項目,但由於安全問題,我無法添加服務引用(通過安全問題我的意思是服務只響應一個IP地址,這是我們客戶的服務器的IP地址。)有沒有辦法生成一個類,如使用「Ildasm.exe」該particaluar網絡服務?沒有添加引用的Web服務?
回答
你並不需要添加Web服務引用與Web服務代碼玩:可以手動生成類如玩:
Wsdl.exe用/out:d:/Proxy.cs /訂單http://localhost:2178/Services.asmx
然後您可以手動將此文件添加到您的項目。
打我:)參考 - > http://msdn.microsoft.com/en-us/library/ms155134.aspx – SeanCocteau 2012-02-28 13:45:38
我建議看看svcutil.exe – 2012-02-28 14:03:29
你可以使用這個類。我不記得我在哪裏找到基本代碼,我添加了一些方法並在之前轉換爲類。
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
是否有可能調用web api方法來使用它調用方法? – altandogan 2015-10-13 20:21:43
請分享您的想法http://stackoverflow.com/questions/40394297/consume-web-service-dynamically-using-httpwebrequest-without-service-reference – 2016-11-03 06:43:36
是,如果你不想加入參考wsdl.exe /out:d:/Proxy.cs /order
將替代
可以動態改變一個服務引用的網址:
var service = new MyService.MyWSSoapClient();
service.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:8080/");
下面是如何從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);
希望這有助於。
- 1. 添加Web服務引用沒有引用它的所有對象
- 2. 添加對Windows服務的Web引用
- 3. 添加對Web服務的引用
- 4. 如何在沒有Web服務URL的情況下添加Web引用?
- 5. 添加web服務引用到eclipse
- 6. 添加Web服務引用後引起的不祥引用
- 7. 添加對ASP.NET Web API服務的服務引用
- 8. Visual Studio 2005中 - 添加Web引用的安全web服務
- 9. 添加服務引用不適用於沒有SVC文件的WCF服務
- 10. 添加服務引用Axis服務
- 11. 沒有Web服務
- 12. Web服務與Web服務引用DLL
- 13. 在C#4.0中不添加服務引用的情況下使用Web服務
- 14. Web服務引用
- 15. wcf服務在添加服務引用時沒有配置文件
- 16. 引用Ashx Web服務 - 使用Web還是服務引用?
- 17. 添加標題信息到我的Web服務引用請求
- 18. 如何將標籤添加到.NET中的Web服務引用?
- 19. 將肥皂頭添加到c#中的Web服務引用中#
- 20. Xamarin表單添加對Web服務的引用
- 21. 在WP7中添加Web服務引用奇怪的問題
- 22. 添加對Web服務的程序集引用
- 23. NuGet包:如何添加對web服務的引用?
- 24. 任何人都可以將服務引用添加到我的Web服務?
- 25. 將服務引用添加到我無權訪問的web服務
- 26. 不使用添加引用方法消費SOAP Web服務
- 27. 向控制檯應用添加Web服務引用
- 28. 無法使用basicHttpBinding將Web引用添加到WCF服務庫
- 29. WCF服務中添加的新功能不反映添加Web引用
- 30. WCF服務的Web引用
如果服務只響應某些IP地址,您如何計劃測試您的代碼?我的建議是從它的擁有者那裏獲得服務的WSDL,並且你應該能夠運行svcutil來生成與它交互的類。 – Rich 2012-02-28 22:50:51