Network Programming (I) SOAP, WSDL, Web Service

Source: Internet
Author: User

During this time, in order to unify your thoughts on network programming, close up this series of tutorials. If there are any mistakes, please correct them. Thank you.

Prerequisites: to familiarize yourself with the following tutorials, you must have the following knowledge: SOAP, WSDL, and Web Service.

The best thing to know about these three things is:

Soap: http://www.w3school.com.cn/soap/index.asp

WSDL: http://www.w3school.com.cn/wsdl/index.asp

Web Service: http://www.w3school.com.cn/webservices/index.asp

Http://www.w3school.com.cn is really good, easy to understand.

Objective: To obtain network data through soap and implement it using the request <--> response method. Repost a simple method, and then analyze the shortcomings.

The detailed steps are in English. I will not translate them here. Why?

I needed to access a soap-based Web Service from the iPhone. There are a wide variety of opinions on how to go about this: "One word: Don't"
Was one poster's reply to a related question at stackoverflow.com. some people suggest writing one's own routines, while others suggest trying various code-Generating tools. one I found was called gsoap, but apparently there is a fair amount of work yet
Do to get it working with the iPhone. I found wsdl2objc which generates objective-C code from a WSDL so you can call soap services. I want to show you how I used wsdl2objc to access a SOAP based Web service.

Here's what you need to get started:

* Wsdl2objc. I'm going to use WSDL2ObjC-0.7-pre1.zip for this tutorial. You can download from this address: http://code.google.com/p/wsdl2ob
... IP & can = 2 & Q =, enter the location of the Code to be generated in the following line. The generated code will be used later to complete the SOAP request and data reply. --- Smking.
* A web service to consume. For this tutorial, we'll use the "localtime" Service (http://www.ripedevelopment.com/webservices/LocalTime.asmx)
Which will, given a zip code, provide the local time for that zip code.


Let's go.

1. Generate the stubs with wsdl2objc.
Start wsdl2objc. In the first field, enter the name of the source WSDL. You can specify a local file, or a Web address. For here, enter http://www.ripedevelopment.com/webservices/LocalTime.asmx? Wsdlin
The second field, specify a directory in which wsdl2objc will create the new files. you can use the Browse button to specify a directory. when you click "parse WSDL", you will see a few messages, one of which shocould say it is generating Objective C code
The output directory. Check the target directory to make sure your files are there.

Create an xcode project, and use a button and textview to display the returned data for convenience. If you do not understand the specific process, you can check the help information about xcode first.

Now wire the button's touch up inside event to the buttonpressed action, and connect the field's outlet to field. Save and close interface builder.

Go ahead and build and run, just to make sure everything's OK so far. I just CT it is, but it doesn't hurt to check.

3. Pull in the wsdl2objc generated code.
Let's pull in the Code wsdl2objc wrote for us: in "other sources," Add a new group named "localtime". With finder, drag all the files that it created
Into the new group. Tell xcode to copy the items into the destination folder.

Take a look at localtime. H. There's a ton of stuff there: definitions for bindings, responses, requests, etc. This Code corresponds to the data
That wsdl2objc pulled out of localtime's WSDL.

Before compiling from this point, make sure you have followed the instructions in the wiki page usageinstructions (ie, the linker flags Properties
And frameworks). You find usageinstructions at the wsdl2objc project home page. Incidentally, the first character of the other C flags property is the capital letter I as in India. You're welcome.

To sum up, the third step is to drag the code to the project and add the framework: cfnetwork. Framework and libxml2.dylib.

Enter "/usr/include/libxml2" under header search paths under targests/build settings (note that no quotation marks are required ).

4. Write.
Let's write some code. Open the. m for your controller. First thing you need to do is # import "localtime. H" Now let's fill in the buttonpressed
Function. Here it is:


-(Ibaction) buttonpressed :( ID) sender {
Localtimesoapbinding * Binding = [[localtime localtimesoapbinding] initwithaddress: @ "http://www.ripedevelopment.com/webservices/LocalTime.asmx"];
Binding. logxmlinout = yes; // to get logging to the console.

Localtime_localtimebyzipcode * request = [[localtime_localtimebyzipcode alloc] init];
Request. zipcode = @ "29687"; // insert your zip code here.

Localtimesoapbindingresponse * resp = [binding localtimebyzipcodeusingparameters: request];
For (ID mine in resp. bodyparts)
{
If ([mine iskindofclass: [localtime_localtimebyzipcoderesponse class])
{
Field. Text = [mine localtimebyzipcoderesult];
}
}
}

The first thing we do is create a binding, and associate it with ripedevelopment's Web service endpoint. We then create a request object, fill in
The blanks, and then call the service. The service returns a localtimesoapbindingresponse, which we save in resp.

Localtimesoapbindingresponse has a field called bodyparts. The code runs through bodyparts, looking for the response to the call. It then stores
The result in the field for us to see.

5.
Run. Run it.
Save everything. Under the run menu, bring up the console, then build and run. Click the "test" button on the simulator. You shocould see the outbound
Request, a response of 200, then... Uh oh, "unexpected response MIME type to soap call: text/XML"

What we're re going to do here is switch the MIME type from application/soap + XML to text/XML. You can use xcode's search and replace; You shocould find
Four occurrences of application/soap + XML change them all to text/XML.

Now when you rerun the app, and click the "test" button, you shoshould see the messages in the console, and then you shoshould see the date appear in
The field.

So there you have it. Now I know I didn't show how to make an asynchronous call, nor did I talk about complex types. That's what next time is.

To sum up this method, it is obvious that the code used is relatively simple, although the introduced code is complicated.

However, I personally think that if you only need to call these requests several times to the same server, that is, there are only a few requests. when the business is relatively simple, the above method can be used and can be tolerated, because as you can see, there are several new classes localtimesoapbindingresponse, localtimesoapbinding,
In the process of usage, encoding is not that easy, because you may depend on these classes, and you have to understand these classes.

However, it is simple and easy to use.

In order not to rely on the code generated by this tool, we will talk about how to directly use the original code or IOS
To do the same thing.

(PS: the above Code should be thread-based, but here to simplify
.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.