Call WebService using HTTPWebRequest object - c#

i want to consuming a web service with HTTPWebRequest object the web service may be written using java.(doon't want to consume adding web reference)

Marcin provides a sample in his blog: Link .
Though it's consuming a .net based web service, but it doesn't make difference from the consumer's point of view.
The basic idea is you need to compose a soap request message and send it to server properly.

Related

C# program to call existing web API

How can I call / use web API that supports SOAP and REST? I want to write a program in C# that will use this web API. I didn't get much information on the internet. So far, I have created C# project and added the web service reference (WSDL) to my program. Now how can I use the web API. How to send request or receive response? Can you please refer me some good tutorials ?
The HttpClient class is a good starting point. Also there is the EasyHttp library.
Code sample HttpClient: http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee
Introduction to EasyHttp: http://devlicio.us/blogs/hadi_hariri/archive/2011/01/16/easyhttp.aspx
Adding web service reference creates client classes that can be used to communicate with web service. All created classes will be in new namespace, You have to look for client proxy class (full name depends on WebService name). Create instance of this class. Calling its methods will call WebService. Here you can read about using Web Services in C#

how to make a service, SOAP web service in .net?

Please forgive me for this basic and a little theoretical question as I dont know much about web services.
I m not refering WCF service, I am reffering simple service in .net / C#. I want to know how to know is it soap or rest service ?
How we can change this type from Soap to Rest and vice versa ?
Thanks
XML Web Services (aka classic/legacy ASMX web services) should not be used for active development. If you must, there is a nice walkthrough on MSDN for adding Web references in more recent versions of Visual Studio (> 2005).
On the other hand, if your web service is truly Restful then you won't be able to create the equivalent of a service reference to it. You'll need to either use the HttpWebRequest, WebClient, or the new HttpClient from .NET 4.5 (also available from the Rest starter kit which is depreciated as well).
As an alternative if you are looking to implement a client that is able to handle both situations, I would recommend HttpWebRequest to POST to the SOAP (non-WCF) service. The problem with this method is you'll likely have to wrap the request in the SOAP wrapper yourself. Luckily there are examples of doing so on the net that you can at least use as a starting point.
ASMX services are build upon SOAP. REST is simply a HTTP based, You can access(or call) your business resources the way you access the normal URLs.
For ex in products catalog system, by using asmx you create set of functions to add,update,delete products. like addProduct(),updateProduct, etc..
But in REST, you will be having single point of access, like http:\mysystem\prodcuts. To retrieve,add,update,delete products, you will be using respective HTTP verbs (GET,POST,PUT,DELETE) on the same URL.
so,technically it's not possible to convert asmx(SOAP) service to rest...

How could I use C# to send and receive data from Web Service directly?

As a tester, i was asked to test a web service via C#. I've no idea how to use C# directly to send data to Web Service. Could you please give some samples about it? BTW, please do not use the proxy method.
Thanks
Sut
I'd recommend using SOAPUI if you are trying to test SOAP web services directly. Trying to call them using HTTP requests and then parsing the resulting SOAP is going to be quite a chore if you have to use C# and not use the imported proxies.
Note: You'd be much better of if you talk to whoever asked you to do something... instead of asking strangers.
In most cases call to web service is simple HTTP POST or GET. There are plenty of ways to perform it directly - i.e. "How to: Send Data Using the WebRequest Class" ( http://msdn.microsoft.com/en-us/library/debx8sh9.aspx).
And response from web service is generally XML - again plenty of classes to read including XmlDocument and XDocument.

When I Make my (ASMX) web service accept/return JSON instead of XML, is it no longer considered SOAP?

Just trying to wrap my head around SOAP vs REST. We currently have some asmx web services, mostly used between our own JavaScript and server code (not a public API). When I specify my method as a ScriptService and specify a ResponseFormat of Json, is it still considered just a SOAP service? It still doesn't feel RESTful to me, but maybe thats because of the way my "resources" are designed (not well/fully represented by rest).
EDIT: Reading more I might be confusing the format (JSON vs XML) with the fact that most descriptions of the SOAP protocol tie in XML. For example, wikipedia states:
It relies on Extensible Markup
Language (XML) for its message format
To me logically that says if I'm using JSON I must not be using SOAP.
This isn't exactly what you asked, but ASMX services are not RESTful if you're calling them from JavaScript and retrieving JSON. You must make a POST request to ASMX services to get JSON out of them, even if the request is idempotent and only retrieves data. In a RESTful API, a GET request would be used in that case, not POST.
That's not to say that the lack of RESTfulness is an actual problem for a private API. I've found ASMX services as a JSON-based service layer for AJAX callbacks works great in practice.
You define what kind of requests are made to you web service (asmx). Many protocols are allowed:
HTTP POST,
HTTP GET,
SOAP 1.1,
SOAP 1.2,
etc... OR you can block any of them.
When you call the web service with javascript you can use POST or GET. It doesn't matter. The trick is what type of content you tell the service to return in these calls. You can tell the service to send you JSON or you can tell the service to send you XML.
When you create a service client in Visual Studio to connect to a ASMX service, Visual studio will try to access the WSDL for the service and the client will be in charge of generating the SOAP envelopes to communicate with the service and in this case you will send and receive XML because thats what the client and server have agreed to use to communicate.

To consume a RESTful web service in C#, should I use HttpWebRequest?

How do I consume a RESTful web service in C# code?
NOTE: by RESTful, I mean non-SOAP. For example, in the flickr API you can call their web service as follows to return JSON data:
http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json
Do I simply use HttpWebRequest?
What if I'm using a web service that takes POST parameters? Would that use HttpWebRequest also?
Is there any benefit to using WCF on the consumer side?
WCF provides a unified programming model for communication, so if you later decide that you do not want to use REST or you want to provide an extra type of endpoint for example SOAP, you only need tp change the configuration.
Take a look at REST for WCF:
http://msdn.microsoft.com/en-us/netframework/cc950529.aspx
Their is a demonstration and labs in the .NET Framework 3.5 Enhancements Training Kit, which provides two labs based around creating, servicing and consuming RESTFul services. The above link is the best MS combined resources of REST services.
Bob.

Categories