How to call WebServices with ASP. NET at run time?

Source: Internet
Author: User

1. Generally, we use httpwebrequest to call WebServices.

Create WebServices:

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Script. Services;
Using system. runtime. serialization;

/// <Summary>
/// Summary description for customerwebservice
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow this web service to be called from script, using ASP. NET Ajax, uncomment the following line.
// [System. Web. Script. Services. scriptservice]
[Scriptservice]
Public class customerwebservice: system. Web. Services. WebService
{
Public customerwebservice ()
{
// Uncomment the following line if using designed components
// Initializecomponent ();
}

// [Webmethod]
// Public String helloworld (){
// Return "Hello World ";
//}
[Scriptmethod]
[Webmethod]
Public String register (long ID, string data1)
{
Return "Id. Customer ";
}
[Scriptmethod]
[Webmethod]
Public String helloworld (person P)
{
Return P. Name;
}
}
[Datacontract]
Public class person
{
[Datamember]
Public string name {Get; set ;}
}

 

In IE, enter the WebServices address (http: // localhost: 8485/mysite/customerwebservice. asmx? OP = helloworld), you can see the SOAP message format that calls the services.

Code

Post/mysite/customerwebservice. asmx HTTP/1.1
HOST: localhost
Content-Type: text/XML; charset = UTF-8
Content-Length: Length
Soapaction: "http://tempuri.org/HelloWorld"

<? 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>
<Helloworld xmlns = "http://tempuri.org/">
<P>
<Name> string </Name>
</P>
</Helloworld>
</Soap: Body>
</Soap: envelope>

 

2. Call WebServices

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. net;
Using system. IO;
Using system. Security. cryptography. x509certificates;
Using system. net. Security;
Using system. text;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
Servicepointmanager. servercertificatevalidationcallback = new system. net. Security. remotecertificatevalidationcallback (onvalidationcallback );
Try
{
// String Param = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> ";
Stringbuilder strb = new stringbuilder ();
Strb. append ("<soap: envelope xmlns: xsi = \" external "xmlns: XSD = \" http://www.w3.org/2001/xmlschema\ "xmlns: Soap = \" external "> ");
Strb. append ("<soap: Body> ");
Strb. append ("Strb. append ("<p> ");
Strb. append ("<Name> personss </Name> ");
Strb. append ("</P> ");
Strb. append ("Strb. append ("</soap: Body> ");
Strb. append ("</soap: envelope> ");
String Param = strb. tostring ();
Byte [] BS = encoding. utf8.getbytes (PARAM );
Httpwebrequest myrequest =
(Httpwebrequest) webrequest. Create (
"Http: // localhost: 8485/mysite/customerwebservice. asmx ");
Myrequest. method = "Post ";
Myrequest. contenttype = "text/XML; charset = UTF-8 ";
Myrequest. headers. Add ("soapaction", "http://tempuri.org/HelloWorld ");
Myrequest. contentlength = BS. length;
Using (Stream reqstream = myrequest. getrequeststream ())
{
Reqstream. Write (BS, 0, BS. Length );
}
Using (httpwebresponse myresponse = (httpwebresponse) myrequest. getresponse ())
{

Streamreader sr = new streamreader (myresponse. getresponsestream (), encoding. utf8 );
String responsestring = Sr. readtoend ();
Response. Write ("Result:" + responsestring );
}
}
Catch (exception ex)
{
Response. Write (system. datetime. Now. tow.timestring ()
+ "Lbs exception:" + ex. Message );
Response. Write (ex. stacktrace );
}
}
Public bool onvalidationcallback (Object sender, x509certificate cert, x509chain chain, sslpolicyerrors errors)
{
Return true;
}

}

 

If 500 internal server error occurs:

Servicepointmanager. servercertificatevalidationcallback = new system. net. Security. remotecertificatevalidationcallback (onvalidationcallback );
Public bool onvalidationcallback (Object sender, x509certificate cert, x509chain chain, sslpolicyerrors errors)
{
Return true;
}

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.