C# Web Service 不添加引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9482773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Service without adding a reference?
提问by ward87
I have 3 web services added to service references in a class library.(This is a sample project for an API use) I need to move these into my projectbut i cannot add the service references because of the security issues(By security issues i mean the service only responds to one ip address and that is the ip address of our customer's server.) Is there a way to generate a class like using "Ildasm.exe" for that particaluar web service?
我有 3 个 web 服务添加到类库中的服务引用。(这是一个 API 使用的示例项目)我需要将这些移动到我的项目中,但由于安全问题我无法添加服务引用(通过安全问题我意味着该服务只响应一个 ip 地址,即我们客户服务器的 ip 地址。)有没有办法为该特定 Web 服务使用“Ildasm.exe”生成一个类?
采纳答案by Marcin
You don't need to add web service reference to play with web service code: You can manually generate class to play with e.g.:
您无需添加 Web 服务引用即可使用 Web 服务代码:您可以手动生成要使用的类,例如:
wsdl.exe /out:d:/Proxy.cs /order http://localhost:2178/Services.asmx
wsdl.exe /out:d:/Proxy.cs /order http://localhost:2178/Services.asmx
And then you can add this file manually to your project.
然后您可以手动将此文件添加到您的项目中。
回答by arunes
You can use this class. I didn't remember where i found the basic code, i added some methods and convert to class before.
你可以使用这个类。我不记得我在哪里找到基本代码,我之前添加了一些方法并转换为类。
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;
}
}
}
And you can use like this
你可以像这样使用
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
回答by Peru
Yes if you dont want to Add reference wsdl.exe /out:d:/Proxy.cs /orderwould be the alternative
是的,如果您不想添加参考 wsdl.exe /out:d:/Proxy.cs /order将是替代方案
回答by Adam Butler
You can dynamically change the url of a service reference:
您可以动态更改服务引用的 url:
var service = new MyService.MyWSSoapClient();
service.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:8080/");
回答by Mike Gledhill
Here's an example of how to call a "GET" web service from your C# code:
以下是如何从 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;
}
Simply pass it a URL, and it'll return a string containing the response. From there, you can call the JSON.Net "DeserializeObject" function to turn the string into something useful:
只需向它传递一个 URL,它就会返回一个包含响应的字符串。从那里,您可以调用 JSON.Net " DeserializeObject" 函数将字符串转换为有用的内容:
string JSONresponse = CallWebService("http://www.inorthwind.com/Service1.svc/getAllCustomers");
List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(JSONresponse);
Hope this helps.
希望这可以帮助。

