C # Call and implement WebService, pure hand-built! _c# Tutorial

Source: Internet
Author: User

Why do you want to do it purely by hand, because the other side is done in C + +, I followed the WSDL they gave to achieve a webservice, and they can not be called at all. The question of whether they call the problem or the WSDL definition is not a test.

The weak side of the sad urge ... We can only go with them. Don't mention it.

First use C # to call each other's webservice.
Adding Web service references is invalid because of the C + + implementation used by the other party ... Adding WSDL directly to the caller does not recognize ...

Had to hand-built, but fortunately, C # strong enough.

The HttpWebRequest class can simply implement the WebService invocation.

First create SOAP package content manually

Copy Code code as follows:

string soap =
"<soapenv:envelope xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:down=\" http:// Down.wsdl.position.mdd.ailk.com\ "xmlns:in=\" http://in.object.down.wsdl.position.mdd.ailk.com\ ">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<TestFunc>"
+ "<object>test message</object>"
+ "</TestFunc>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";



Constructing HttpWebRequest Objects

Copy Code code as follows:

HttpWebRequest request = (HttpWebRequest) httpwebrequest.create ("http://localhost:8088/");
Request. method = "POST";
Request. ContentType = "Text/xml; Charset=utf-8 ";
Request. Headers.add ("SOAPAction", "/testfunc");
byte[] BTS = Encoding.UTF8.GetBytes (SOAP);


Send Request

Copy Code code as follows:

Request. ContentLength = data. Length;
Stream writer = Request. GetRequestStream ();
Writer. Write (data, 0, data. Length);
Writer. Close ();


Get return information

Copy Code code as follows:

StreamReader sr = new StreamReader (Request. GetResponse (). GetResponseStream (), Encoding.UTF8);
String retxml = Sr. ReadToEnd ();
Sr. Close ();

Then, as defined by the WSDL, parsing the returned XML string is good.

Of course, hand-played to create is tired ...

If it is a standard webservice, direct a string of str = Serv.testfunc ("");

Don't mention it.

C # Implementation WebService to the other caller
As I understand it, the WebService is soap, which is essentially a TCP short connection.

The solution is to use C # to implement a TCP server, and then manually analyze the request content.

Copy Code code as follows:

Start listening port
MyListener = new TcpListener (Ipaddress.parse (LISTENIPADDR), listenport);
Mylistener.start ();


Receive the connection and get the request content
Copy Code code as follows:

Accept New Connection
Socket mysocket = Mylistener.acceptsocket ();
String sbuffer = "";
byte[] breceive = new byte[2048];

Receive Request content
int i = mysocket.receive (breceive, breceive.length, 0);
Sbuffer = Encoding.ASCII.GetString (breceive);

Process only "POST" request types
if (sbuffer.substring (0, 4)!= "POST")
{
Return
}


Intercept the Soap:body section
Copy Code code as follows:

string soap = "<?xml version=\" 1.0\ "encoding=\" utf-8\ ">\n";
int istartpos = Sbuffer.indexof ("<soap:Body>", 1);
int istoppos = Sbuffer.indexof ("</soap:Body>", 1);

if (Istartpos > 0)
{
Soap = soap + sbuffer.substring (istartpos, Istoppos-istartpos + 9);
}



Analyze request content Here's a little bit.

The structure of the returned message is as follows.

Construct the message body content first

Copy Code code as follows:

string ret = string. Format (
""
+ "<soapenv:envelope xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:down=\" http:// Down.wsdl.position.mdd.ailk.com\ "xmlns:out=\" http://out.object.down.wsdl.position.mdd.ailk.com\ ">\n"
+ "<soapenv:header/>\n"
+ "<soapenv:body>\n"
+ "<testrsp>\n"
+ "<object resultcode=\" 0\ "/>\n"
+ "</testrsp>\n"
+ "</soapenv:body>\n"
+ "</soapenv:Envelope>"
);
byte[] bytes = Encoding.ASCII.GetBytes (ret);



Construct message headers

Copy Code code as follows:

Structural head
String Smimetype = "text/html";
String shttpversion = "http/1.1";
String sbuffer = "";

if (smimeheader.length = 0)
{
Smimeheader = "text/html"; Default text/html
}

Sbuffer = sbuffer + shttpversion + "OK" + "\ r \ n";
Sbuffer = Sbuffer + "server:cx1193719-b\r\n";
Sbuffer = Sbuffer + "Content-type:" + smimetype + "\ r \ n";
Sbuffer = Sbuffer + "accept-ranges:bytes\r\n";
Sbuffer = Sbuffer + "Content-length:" + itotbytes + "\r\n\r\n";

Byte[] heads = Encoding.ASCII.GetBytes (Sbuffer);


Then send it to each other and close the socket.

Copy Code code as follows:

Mysocket.send (heads, heads. Length, 0);
Mysocket.send (bytes, bytes. Length, 0);

Mysocket.close ();

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.