WebService using basic techniques

Source: Internet
Author: User

First, the basic concept of WebService

WebService, also known as Xmlweb SerVice WebService, is a lightweight, independent communication technology that can receive requests from other systems on the Internet or intranet. is: the software (service) provided on the Web by soap, using a WSDL file (description), and registering through (UDDI).

XML: (extensible Markup Language) Extensible Markup Language. Temporary data processing for short-term, web-oriented network, is the basis of soap.

Soap: Simple Object access Protocol protocol. is the communication protocol for XML WEB Service. When a user finds your WSDL description document through UDDI, he can invoke one or more of the actions in the Web service that you set up by soap. SOAP is a specification for calling methods in the form of XML documents that can support different underlying interfaces, such as HTTP (S) or SMTP.

WSDL: (Web Services Description Language) A WSDL file is an XML document that describes a set of SOAP messages and how to exchange them. In most cases, it is automatically generated and used by the software.

UDDI (Universal Description, Discovery, and integration) is a new project primarily for Web service providers and users. Before a user can invoke a Web service, it is important to determine which business methods are included in the service, to find the called interface definition, and to prepare the software on the server side, and UDDI is a mechanism to guide the system through the description document to find the appropriate service. UDDI uses the SOAP message mechanism (standard xml/http) to publish, edit, browse, and find registration information. It uses XML format to encapsulate various types of data and send it to the registry or to the registry to return the required data.

The relationship between HTTP soap

http: is a standard (TCP) for client and server-side requests and responses. The purpose of the HTTP protocol is to provide a way to publish and receive HTTP pages.

Client-to-server interaction with the HTTP protocol: a request is initiated by an HTTP client to establish a TCP connection to the server-specified port (by default, port 80). The HTTP server listens on that port for requests sent by the client. Once the request is received, the server (to the client) sends back a status line, such as "http/1.1 OK", and (in response) message, the message body may be the requested file, error message, or some other information.

SOAP protocol: It describes a lightweight protocol for exchanging information in a decentralized or distributed environment. SOAP, based on the HTTP protocol, is an XML-based protocol.

Different: is the underlying communication protocol, the request package format is different, the SOAP package is XML format, HTTP plain text format.

Relationship: SOAP is a communication protocol, SOAP on the basis of the HTTP protocol, the request parameters written as XML, placed on the HTTP body on the submission of a Web service server (servlet,asp, etc.) processing is completed, The result is also written as XML as response back to the client, in order to make the client and the Web service can correspond to each other, you can use WSDL as a description of this mode of communication, using the WSDL tool can automatically generate WS and client framework files, SOAP has the ability to serialize complex objects into XML.

Second, the basic method of realizing WebService communication in C #

1. The reference using System.Net uses the HttpWebRequest with the HttpWebResponse implementation. This method is less complicated than before.

The HttpWebRequest class mainly uses HTTP protocol and server interaction, usually through get and post two ways to obtain and submit data;

Get mode:

The post mode get method completes the data submission by appending the parameter in the network address, for example in the address Http://www.jb51.net/?hl=zh-CN, the previous section http://www.jb51.net represents the data submission URL, the later part hl= ZH-CN represents an additional parameter, where HL represents a key (key) and ZH-CN represents the value of the key (value).

The C # code is as follows:

HttpWebRequest wrequest = (HttpWebRequest) webrequest.create ("Http://www.jb51.net?hl=zh-CN");
Wrequest. Method = "GET";
HttpWebResponse WebResponse = (httpwebresponse) wrequest. GetResponse ();
StreamReader sr = new StreamReader (WebResponse.GetResponseStream (), Encoding.UTF8);
String Data= Sr. ReadToEnd ();
return data;

Another: Send with parameters

Protected Resultinfo Addinstrumetstatus (String instrument_id, string status, String message)
{

Try
{
String postdatastr = instrument_id + "&" + Status + "&" + message;
String URL =url + (Postdatastr = = ""? "": "?") + Postdatastr;
HttpWebRequest myrequest = (HttpWebRequest) webrequest.create (URL);
Myrequest.method = "POST";
Myrequest.contenttype = "Application/json;charset=utf-8";
Myrequest.mediatype = "Application/json";
myrequest.accept = "Application/json";
if (!string. IsNullOrEmpty (this._username) &&!string. IsNullOrEmpty (This._password))
{
MYREQUEST.HEADERS.ADD ("Authorization", "Basic" + convert.tobase64string (Encoding.ASCII.GetBytes (This._username +): "+ This._password)));
}
Receive data
HttpWebResponse myresponse = (HttpWebResponse) myrequest.getresponse ();
if (Myresponse.statuscode = = httpstatuscode.created)
{
Result. Issuccess = true;
using (Stream s = Myresponse.getresponsestream ())
{
StreamReader Mystreamreader = new StreamReader (s, Encoding.UTF8);
Result. Resultdata = Mystreamreader.readtoend ();
Mystreamreader.close ();
Myresponse.close ();
S.close ();
Myrequest.abort ();
}
}

else if (Myresponse.statuscode = = Httpstatuscode.ok)
{
Result. Validatemessages = new dictionary<string, string> ();
Fill valid messages from response content

}
Else
{
Result. Resultmessage = String.Format ("Gettaskconfig failed {0}", Myresponse.statuscode);
}
}
catch (Exception ex)
{
throw new Exception ("The Zlims API connected Exception," + ex. ToString ());
}
return result;

Post mode:

String strjson;//the data sent
byte[] Payload = System.Text.Encoding.UTF8.GetBytes (Strjson);
Resultinfo result = new Resultinfo ();
Try
{
HttpWebRequest myrequest = (HttpWebRequest) webrequest.create (URL);
Myrequest.method = "POST";
Myrequest.contenttype = "Application/json;charset=utf-8";
Myrequest.mediatype = "Application/json";
myrequest.accept = "Application/json";
if (!string. IsNullOrEmpty (this._username) &&!string. IsNullOrEmpty (This._password))
{
MYREQUEST.HEADERS.ADD ("Authorization", "Basic" + convert.tobase64string (Encoding.ASCII.GetBytes (This._username +): "+ This._password)));
}
Myrequest.contentlength = payload. Length;
Send data
using (Stream Writer = Myrequest.getrequeststream ())
{
Writer.write (payload, 0, payload. Length);
Writer.close ();
}
Receive data
HttpWebResponse Myresponse;
Myresponse = (HttpWebResponse) myrequest.getresponse ();
if (myresponse! = null)
{
Result. Issuccess = true;
using (Stream s = Myresponse.getresponsestream ())
{
StreamReader Mystreamreader = new StreamReader (s, Encoding.UTF8);
Result. Resultdata = Mystreamreader.readtoend ();
Mystreamreader.close ();
Myresponse.close ();
S.close ();
Myrequest.abort ();
}
}

else if (Myresponse.statuscode = = Httpstatuscode.ok)
{

}
Else
{
Result. Resultmessage = String.Format ("Gettaskconfig failed {0}", Myresponse.statuscode);
}
}
catch (Exception ex)
{
throw new Exception ("The Zlims API connected Exception," + ex. ToString ());
}
return result;

}

WebService using basic techniques

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.