ASP. NET releases WebService

Source: Internet
Author: User
ArticleDirectory
    • Test
    • HTTP 1.1
    • HTTP 1.2
    • HTTP POST

ASP. NET releases WebService

I. Release

    1. Create a webapplication project.
    2. Add the WebService file (Suffix: asmx) to the project testwebservice and enter the name.

Open the file you just added.CodeAs follows:

 

Code

  Using  System;

Using System. collections;

Using System. componentmodel;

Using System. Data;

Using System. LINQ;

Using System. Web;

Using System. Web. Services;

Using System. Web. Services. Protocols;

Using System. xml. LINQ;

Using System. Web. Script. Services;



Namespace Sample

{

/// <Summary>

/// Summary Description for service1

/// </Summary>

[WebService (namespace = " Http://tempuri.org/ " )]

[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]

[Toolboxitem ( False )]

// To allow this web service to be called from script, using ASP. NET Ajax, uncomment the following line.

[System. Web. Script. Services. scriptservice]

Public Class Service1: system. Web. Services. WebService

{



[Webmethod]

Public String Helloworld ()

{

Return " Hello World " ;

}



[Webmethod]

Public Int Add ( Int A, Int B)

{

Return ( + B );

}



[Webmethod]

Public Int Minus ( Int A, Int B)

{

Return ( - B );

}

}

}

 

Note the following:

(1). The Code of a class is generated on this page, which inherits from system. Web. Services. WebService.

For example:

 

  Public ClassService1: system. Web. Services. WebService

(2). If you want to use Asp.net Ajax [system. Web. Script. Services. scriptservice]

 
  //To allow this web service to be called from script, using ASP. NET Ajax, uncomment the following line.

[System. Web. Script. Services. scriptservice]

(3). To publish a function to WebService, add[Webmethod]

 

Ii. Test

Directly run the page and the following page appears:

We can see that service1 is the name of this class, And all published WebService names are listed in the header of the page. The following text describes WebService. If you have not modified the namespace in asmx code, we recommend that you modify the namespace.

Service1

 

The following operations are supported. For more information about the formal definition, seeService Description.

    • Add
    • Helloworld
    • Minus

ThisWebService UsageHttp://tempuri.org/As the default namespace. 

Suggestions:PublicXML Web ServicesBefore that, change the default namespace. 

Each XML Web Service requires a unique namespace for client applicationsProgramIt can be separated from other service areas on the web. Http://tempuri.org/can be used for XML Web Services at the development stage, where published XML Web Services should use a more permanent namespace.

The namespace you control should be used to identify XML Web Services. For example, you can use the company's Internet domain name as part of the namespace. Although many XML Web Services namespaces seem to be URLs, they do not need to point to actual resources on the web. (The namespace of XML Web Services is uri .)

When using ASP. NET to create XML Web Services, you can use the namespace attribute of WebService to change the default namespace. The WebService feature is applicable to classes that contain XML Web Services methods. The following code example sets the namespace to "http://microsoft.com/webservices /":

C #

[WebService (namespace = "http://microsoft.com/webservices/")]

Public class mywebservice {

// Implementation

}

Visual Basic

<WebService (namespace: = "http://microsoft.com/webservices/")> public class mywebservice

'Implementation

End Class

C ++

[WebService (namespace = "http://microsoft.com/webservices/")]

Public ref class mywebservice {

// Implementation

};

For more information about XML Namespaces, seeNamespaces in XML (XMLNamespace)W3C recommendations on.

For more information about WSDL, seeWSDL specification (WSDLSpecifications).

For more information about Uris, seeRFC 2396.

 

Click Add connection to open the Add test page. Enter the parameters and click the [Call] button to obtain the value.

Service1

ClickHereTo obtain the complete list of operations.

Add Test

To test the operation using the http post protocol, click the "call" button.

HTTP 1.1

The following are examples of soap 1.2 requests and responses. ThePlaceholderIt must be replaced with the actual value.

 
Post or service1.asmx HTTP/1.1
 
HOST: localhost
 
Content-Type: text/XML; charset = UTF-8
 
Content-Length:Length
 
Soapaction: "http://tempuri.org/Add"
 
 
 
<? 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>
 
<Add xmlns = "http://tempuri.org/">
<A>Int</A>
 
<B>Int</B>
 
</Add>
 
</Soap: Body>
 
</Soap: envelope>
 
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 = "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>
 
<Addresponse xmlns = "http://tempuri.org/">
<Addresult>Int</Addresult>
 
</Addresponse>
 
</Soap: Body>
 
</Soap: envelope>
HTTP 1.2

The following are examples of soap 1.2 requests and responses. ThePlaceholderIt must be replaced with the actual value.

 
Post or service1.asmx HTTP/1.1
 
HOST: localhost
 
Content-Type: Application/soap + XML; charset = UTF-8
 
Content-Length:Length
 
 
 
<? XML version = "1.0" encoding = "UTF-8"?>
 
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
 
<Soap12: Body>
<Add xmlns = "http://tempuri.org/">
 
<A>Int</A>
 
<B>Int</B>
 
</Add>
 
</Soap12: Body>
 
</Soap12: envelope>
 
HTTP/1.1 200 OK
 
Content-Type: Application/soap + XML; charset = UTF-8
 
Content-Length:Length
 
 
 
<? XML version = "1.0" encoding = "UTF-8"?>
 
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
 
<Soap12: Body>
<Addresponse xmlns = "http://tempuri.org/">
 
<Addresult>Int</Addresult>
 
</Addresponse>
 
</Soap12: Body>
 
</Soap12: envelope>
HTTP POST

The following are examples of http post requests and responses. ThePlaceholderIt must be replaced with the actual value.

 
Post/service1.asmx/addhttp/1.1
 
HOST: localhost
 
Content-Type: Application/X-WWW-form-urlencoded
 
Content-Length:Length
 
 
 
A=String&B=String
 
HTTP/1.1 200 OK
 
Content-Type: text/XML; charset = UTF-8
 
Content-Length:Length
 
 
<? XML version = "1.0" encoding = "UTF-8"?>
 
<Int xmlns = "http://tempuri.org/">Int</Int>

In IE, you should see in the following table. If you only find 3 in Google Chrome, You need to viewSource codeTo see the following content.

<? XML version = "1.0" encoding = "UTF-8"?>

 <Int xmlns ="Http://tempuri.org/">3</Int>

Related Article

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.