I'm trying to consume the Swagger sample Petstore definition .json file at -
https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json
I used VS 2017 > Add > Add REST Api Client functionality. The import is successful and the Swagger Petstore proxy classes are successfully generated and added to my project.
Now I want to write C# code that is actually able to call the Petstore API.I'm unable to find any concrete examples that leverage the proxy classes above.
The way I have worked with REST services has been using the Web Client class in C# -
string sendText = "abc";
WebClient client = new WebClient();
c.Headers[HttpRequestHeader.ContentType] = "text/xml";
result = c.UploadString(url, sendText);
How do I write code that calls the PUT GET POST etc. operation on the Pet Store API?
Related
Coming form the Autorest world, where the Web Client would generate one class per controller, but doesn't seem possible in NSwag using Service Reference within .Net 5
AutoRest generated web clients follow this pattern.
{BaseWebClient}.{ControllerName}.{Methods}
For example:
IWebClient webClient = new WebClient();
webClient.Employee.Get()
In NSwag, it generates all the methods under one class.
webClient.Employee_get();
Can somebody confirm if this is the new way or proper way of generating web clients?
I am consuming a webservice from a Java server. The webservice provides me with some methods that need. Up till now I have been using the method where I added the reference of the webservice in the project explorer, typed the address of the webservice and compiled it. But now I need it to pick up the address of the service at runtime from an xml file or something! is that possible?
There is Url property in generated proxy object that you can set at runtime. Covered in Creating the Web Service Proxy article on MSDN.
Sample from the article (shows how to also set credentials, you may also need to set Proxy):
var rs = new ReportExecutionService();
rs.Url = "http://<Server Name>/reportserver/reportexecution2005.asmx?wsdl";
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
Note that above answer is for case when you don't know exact Url of server till runtime, but you have WSDL/sample server available at design time and able to generate proxy via add web service in VS (or manually).
Alternatively you can call service via other classes implementing "HTTP GET" like HttpClient and configure anything you want, but will need to do your own parsing of results.
Its going to a grt puzzle for me now a days. I have developed a product which is implemented using multiple languages such as a C# Windows app, Titanium iOS app, and a Java application with a team of friends.
I am using a c# web service which is taking parameter of datatype byte[]. I have completed my work on windows app by adding it in service reference.
My Titanium team mate asked me to create some sample code for this web service without using service refrence directly by the url, but instead, either:
Call it with soap or http post methods.
Create a web sevice that they will use with titanium in a easy way
Any other useful idea on how to use the same webservice with titanium
As titanium boy is fresher with titanium right now so I have to do something but I am also stuck and don't know how to suggest him something so I need help from your side.
I suggest you encode your binary data into a Base64 string and send it as such to your C# service. Since you're using SOAP, it would be a very simple solution.
Just use the built in Titanium utilities to encode your data to base64:
// Encode your data
var data = Titanium.Utils.base64encode(dataToSendToWebService);
Now send it using a HTTPClient:
var postDataTOServer = Ti.Network.createHTTPClient({
onload : function(e) {
// If the service returns successfully : true, or false
var isUserAllowed = this.responseText;
},
onerror : function(e) {
// Web service failed for some reason
Ti.API.info(this.responseText);
Ti.API.info('webservice failed with message : ' + e.error);
}
});
// POST
postDataTOServer.open('POST', 'http://yoursite.com/aspwebservice');
// you may have to change the content type depending on your service
// but this is the correct type for binary data
postDataTOServer.setRequestHeader("Content-Type", "application/octet-stream");
// This does a POST to server
postDataTOServer.send(data);
I wrote a web-service. I wrote a website. I want the website BLL code to call the web-service.
I have a config table with this service URL. I inject the web-service URL to the calling code. What web client or socket in C# should I use that can receive a dynamic web-service URL?
I thought to use:
WebClient webClient = new WebClient();
UTF8Encoding response = new UTF8Encoding();
string originalStr = response.GetString(webClient.DownloadData(BLLConfig.Current);
But maybe there is more elegant way?
I'm loading the configs at run time from a DB table.
Here is how I tried to use a web-reference in Visual Studio:
using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(CtId, tempalteIds.ToArray());
foreach (var pair in responsArray)
{
string value = FetchTemplateValue(pair.Key, pair.Value);
TemplateComponentsData.Add(pair.Key, value);
}
}
You can add the URL of the web service as a Web Reference in Visual Studio and then set the Service.URL property to the value from the config
.NET has lots of built-in support for consuming web services... after adding the service reference to your project it generates the necessary code... whcih you can use as is - if you need to configure the URL the generated client class has a URL property which you can set accordingly... for an excellent walkthrough see http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/ and see SOAP xml client - using Visual Studio 2010 c# - how?
I am developing a c# desktop application and using a webservies which is developed in a php application when i try to consume that application. I just add web REference of that web service and try to access throught the following code
WebReference.TestWSDL pdl = new testingApp.WebReference.TestWSDL();
string copy = pdl.verify("testing");
it throws the error when i try to call the method verify. the error is
Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/wsdl/ was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/.
and the web service link was like
http://171.139.101.12/code/index.php/webservice/wsdl
The error you are encountering is informing you that when you invoke the webservice, you are being given the WSDL (Web Service Definition Language) for the service - this is the metadata that describes the service functions, but cannot actually be used to invoke the service. Usually, you access the WSDL by appending either "?wsdl" or "wsdl" to the service URI.
There are two elements to the webservice you are attempting to consume.
The actual service exists at:
http://171.139.101.12/code/index.php/webservice
The metadata describing it, which Visual Studio via wsdl.exe used to generate a proxy, resides here:
http://171.139.101.12/code/index.php/webservice/wsdl
You need to edit the properties of the Web Reference and update the address appropriately. Alternatively, you can alter the properties of the pdl variable, and change the endpoint in code.