Web service without adding a link?

I have 3 web services added to service links in the class library (this is a sample project for using the API). I need to move them to my project , but I can’t add links to the services due to security problems (for security issues, I mean that the service responds to only one IP address, and this is the IP address of our client server. ) Is there a way to create a class, for example, using "Ildasm.exe" for this partial network service?

+6
source share
5 answers

You do not need to add a link to the web service for playback with the web service code: you can manually generate a class for the game, for example:

wsdl.exe / out: d: /Proxy.cs/order http: // localhost: 2178 / Services.asmx

And then you can add this file manually to your project.

+8
source

You can use this class. I don’t remember where I found the base code, I added some methods and converted to a 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 this as

 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 
+32
source

You can dynamically change the service link url:

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

Here is an example of how to call the GET web service from your C # code:

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

Just pass it the url and it will return a string containing the response. From there, you can call the JSON.Net " DeserializeObject " function to turn the string into something useful:

 string JSONresponse = CallWebService("http://www.inorthwind.com/Service1.svc/getAllCustomers"); List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(JSONresponse); 

Hope this helps.

+2
source

Yes, if you do not want to add the wsdl.exe /out:d:/Proxy.cs /order link, there will be an alternative

0
source

Source: https://habr.com/ru/post/909551/


All Articles