Use Java to call. Net WebService instances

Source: Internet
Author: User
Recent projects are related to WebService. Naturally, you need to pay attention to WebService resources.
Http://www.wopos.com/webservice/Weather.asmx? OP = getweather

It is a weather forecast's WebService. According to its output, the weather data should come from the Central Meteorological Bureau's

Http://www.tq121.com.cn/

However, we don't have to worry much about this. We are concerned about how to call this WebService.
First, log on to www.wopos.com/webservice/weather.asmx? OP = getweather. The following SOAP information is displayed:
Request:

The following is the program code:

Post/WebService/weather. asmx HTTP/1.1
HOST: www.wopos.com
Content-Type: text/XML; charset = UTF-8
Content-Length: Length
Soapaction: "http://tempuri.org/getWeather"

<? XML version = "1.0" encoding = "UTF-8"?>
<Soap: envelope xmlns: xsi = "The http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "The http://www.w3.org/2001/XMLSchema" xmlns: Soap = "The http://schemas.xmlsoap.org/soap/envelope/">
<Soap: Body>
<Getweather xmlns = "http://tempuri.org/">
<Mcity> string </mcity>
</Getweather>
</Soap: Body>
</Soap: envelope>

Copy all the XML to create an XML file (common text files can also be used). For future programming

The following is the program code:

...
<Mcity> string </mcity>
...

Change

The following is the program code:

...
<Mcity >$ {city }$ </mcity>
...

Save weathersoap. XML in the same directory of the classes generated later.

Response:

The following is the program code:

HTTP/1.1 200 OK
Content-Type: text/XML; charset = UTF-8
Content-Length: Length

<? XML version = "1.0" encoding = "UTF-8"?>
<Soap: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "H ttp: // www.w3.org/2001/xmlschema" xmlns: Soap = "H ttp: // schemas.xmlsoap.org/soap/envelope/">
<Soap: Body>
<Getweatherresponse xmlns = "H ttp: // tempuri.org/">
<Getweatherresult> string </getweatherresult>
</Getweatherresponse>
</Soap: Body>
</Soap: envelope>

The XML Description of the Response Section is used for subsequent XML explanations.

Next, write the code.

The following is the program code:

Package jaqcy. weatherreport. client;

Import java. Io .*;
Import java.net .*;
Import javax. xml. parsers .*;
Import org. W3C. Dom .*;
/**
*
* @ Author jaqcy
*/
Public class weatherreport
{
Private Static string getsoaprequest (string city) // city indicates the city name of the weather to query.
{
Try
{
Class CLS = object. Class;
Inputstreamreader ISR = new inputstreamreader (CLS. getresourceasstream ("/jaqcy/weatherreport/client/weathersoap. xml"); // read soap information with weathersoap
Bufferedreader reader = new bufferedreader (ISR );
String soap = "";
String TMP;
While (TMP = reader. Readline ())! = NULL)
{
Soap + = TMP;
}
Reader. Close ();
ISR. Close ();
Return soap. Replace ("$ {city }$", city); // replace the original $ {city} $ with the input parameter city.
}
Catch (exception ex)
{
Ex. printstacktrace ();
Return NULL;
}
}
/*
* The inputstream is returned because the document parse method in W3C Dom can
* To accept parameters of the inputstream type, we will explain the XML in the next step.
*/
Private Static inputstream getsoapinputstream (string city) throws exception
{
Try
{
String soap = getsoaprequest (city );
If (soap = NULL)
{
Return NULL;
}
URL url = new URL ("http://www.wopos.com/webservice/Weather.asmx ");
Urlconnection conn = URL. openconnection ();
Conn. setusecaches (false );
Conn. setdoinput (true );
Conn. setdooutput (true );

Conn. setrequestproperty ("Content-Length", integer. tostring (soap. Length ()));
Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8 ");
Conn. setrequestproperty ("soapaction", "/" http://tempuri.org/getWeather "");

Outputstream OS = conn. getoutputstream ();
Outputstreamwriter OSW = new outputstreamwriter (OS, "UTF-8 ");
OSW. Write (SOAP );
OSW. Flush ();
OSW. Close ();

Inputstream is = conn. getinputstream ();
Return is;
}
Catch (exception E)
{
E. printstacktrace ();
Return NULL;
}
}
/*
* Use W3C Dom to explain the returned XML.
*
*/
Public static string getweather (string City)
{
Try
{
Document Doc;
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
DBF. setnamespaceaware (true );
Documentbuilder DB = DBF. newdocumentbuilder ();
Inputstream is = getsoapinputstream (city );
Doc = dB. parse (is );
Nodelist NL = Doc. getelementsbytagname ("getweatherresult ");
Node n = NL. Item (0 );
String weather = n. getfirstchild (). getnodevalue ();
Is. Close ();
Return weather;
}
Catch (exception E)
{
E. printstacktrace ();
Return NULL;
}
}
}

Write a main method to check the result

The following is the program code:

Public static void main (string [] ARGs) throws exception
{
System. Out. println (weatherreport. getweather ("Zhuhai "));
}

The result is as follows:

The following is the program code:

City = Zhuhai, date = 4.02-4.03, figure 1 = Beijing ~ 22 ℃, wind = breeze, UV = weak

The result is a bit messy, but it is easy to separate it.
Adding the weather forecast function to your application is also good for the program's competitiveness, and the implementation is quite simple. Why not?

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.