Dynamic webservice calls the interface and reads the resolution returned results

Source: Internet
Author: User

Dynamic webservice calls the interface and reads the resolution returned results

Webservice publishing generally uses the style of the web service descriptive language file. In the WSDL file, this webservice is exposed to external interfaces for use. Today, we will discuss in detail how to dynamically call and read the resolution returned results.

Here we will give you a specific example of dynamic webservice calling the interface and reading the returned results of parsing. This is very simple and the annotations are also very detailed. For more information, see.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

Using System;

Using System. Collections;

Using System. IO;

Using System. Net;

Using System. Text;

Using System. Xml;

Using System. Xml. Serialization;

Namespace Hishop. Plugins

{

/// <Summary>

/// Use WebRequest/WebResponse to call WebService

/// </Summary>

Public class WebServiceCaller

{

# Region Tip: Instructions for use

// WebServices should support Get and Post calls. Add the following code in web. config:

// <WebServices>

// <Protocols>

// <Add name = "HttpGet"/>

// <Add name = "HttpPost"/>

/// </Protocols>

// </WebServices>

 

// Call example:

// Hashtable ht = new Hashtable (); // Hashtable is the parameter set required by webservice.

// Ht. Add ("str", "test ");

// Ht. Add ("B", "true ");

// XmlDocument xx = WebSvcCaller. QuerySoapWebService ("http: // localhost: 81/service. asmx", "HelloWorld", ht );

// MessageBox. Show (xx. OuterXml );

# Endregion

 

/// <Summary>

/// WebService is required to support Post call

/// </Summary>

Public static XmlDocument QueryPostWebService (String URL, String MethodName, Hashtable Pars)

{

HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (URL + "/" + MethodName );

Request. Method = "POST ";

Request. ContentType = "application/x-www-form-urlencoded ";

SetWebRequest (request );

Byte [] data = EncodePars (Pars );

WriteRequestData (request, data );

Return ReadXmlResponse (request. GetResponse ());

}

 

/// <Summary>

/// WebService is required to support Get call

/// </Summary>

Public static XmlDocument QueryGetWebService (String URL, String MethodName, Hashtable Pars)

{

HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (URL + "/" + MethodName + "? "+ ParsToString (Pars ));

Request. Method = "GET ";

Request. ContentType = "application/x-www-form-urlencoded ";

SetWebRequest (request );

Return ReadXmlResponse (request. GetResponse ());

}

 

/// <Summary>

/// Common WebService call (Soap). The Pars parameter is a String-type parameter name and value.

/// </Summary>

Public static XmlDocument QuerySoapWebService (String URL, String MethodName, Hashtable Pars)

{

If (_ xmlNamespaces. ContainsKey (URL ))

{

Return QuerySoapWebService (URL, MethodName, Pars, _ xmlNamespaces [URL]. ToString ());

}

Else

{

Return QuerySoapWebService (URL, MethodName, Pars, GetNamespace (URL ));

}

}

 

Private static XmlDocument QuerySoapWebService (String URL, String MethodName, Hashtable Pars, string XmlNs)

{

_ XmlNamespaces [URL] = XmlNs; // Add cache to improve efficiency

HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (URL );

Request. Method = "POST ";

Request. ContentType = "text/xml; charsets = UTF-8 ";

Request. Headers. Add ("SOAPAction", "\" "+ XmlNs + (XmlNs. EndsWith ("/")? "": "/") + MethodName + "\"");

SetWebRequest (request );

Byte [] data = EncodeParsToSoap (Pars, XmlNs, MethodName );

WriteRequestData (request, data );

XmlDocument doc = new XmlDocument (), doc2 = new XmlDocument ();

Doc = ReadXmlResponse (request. GetResponse ());

 

XmlNamespaceManager mgr = new XmlNamespaceManager (doc. NameTable );

Mgr. AddNamespace ("soap", "http://schemas.xmlsoap.org/soap/envelope ");

String RetXml = doc. SelectSingleNode ("// soap: Body/*", mgr). InnerXml;

Doc2.LoadXml ("<root>" + RetXml + "</root> ");

AddDelaration (doc2 );

Return doc2;

}

Private static string GetNamespace (String URL)

{

HttpWebRequest request = (HttpWebRequest) WebRequest. Create (URL + "? WSDL ");

SetWebRequest (request );

WebResponse response = request. GetResponse ();

StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );

XmlDocument doc = new XmlDocument ();

Doc. LoadXml (sr. ReadToEnd ());

Sr. Close ();

Return doc. SelectSingleNode ("// @ targetNamespace"). Value;

}

 

Private static byte [] EncodeParsToSoap (Hashtable Pars, String XmlNs, String MethodName)

{

XmlDocument doc = new XmlDocument ();

Doc. loadXml ("<soap: Envelope xmlns: xsi = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap = \" external "> </soap: envelope> ");

AddDelaration (doc );

// XmlElement soapBody = doc. createElement_x_x ("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope ");

XmlElement soapBody = doc. CreateElement ("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope ");

// XmlElement soapMethod = doc. createElement_x_x (MethodName );

XmlElement soapMethod = doc. CreateElement (MethodName );

SoapMethod. SetAttribute ("xmlns", XmlNs );

Foreach (string k in Pars. Keys)

{

// XmlElement soapPar = doc. createElement_x_x (k );

XmlElement soapPar = doc. CreateElement (k );

SoapPar. InnerXml = ObjectToSoapXml (Pars [k]);

SoapMethod. AppendChild (soapPar );

}

SoapBody. AppendChild (soapMethod );

Doc. DocumentElement. AppendChild (soapBody );

Return Encoding. UTF8.GetBytes (doc. OuterXml );

}

Private static string ObjectToSoapXml (object o)

{

XmlSerializer mySerializer = new XmlSerializer (o. GetType ());

MemoryStream MS = new MemoryStream ();

MySerializer. Serialize (MS, o );

XmlDocument doc = new XmlDocument ();

Doc. LoadXml (Encoding. UTF8.GetString (ms. ToArray ()));

If (doc. DocumentElement! = Null)

{

Return doc. DocumentElement. InnerXml;

}

Else

{

Return o. ToString ();

}

}

 

/// <Summary>

/// Set the credential and timeout

/// </Summary>

/// <Param name = "request"> </param>

Private static void SetWebRequest (HttpWebRequest request)

{

Request. Credentials = CredentialCache. DefaultCredentials;

Request. Timeout = 10000;

}

 

Private static void WriteRequestData (HttpWebRequest request, byte [] data)

{

Request. ContentLength = data. Length;

Stream writer = request. GetRequestStream ();

Writer. Write (data, 0, data. Length );

Writer. Close ();

}

 

Private static byte [] EncodePars (Hashtable Pars)

{

Return Encoding. UTF8.GetBytes (ParsToString (Pars ));

}

 

Private static String ParsToString (Hashtable Pars)

{

StringBuilder sb = new StringBuilder ();

Foreach (string k in Pars. Keys)

{

If (sb. Length> 0)

{

Sb. Append ("&");

}

// Sb. Append (HttpUtility. UrlEncode (k) + "=" + HttpUtility. UrlEncode (Pars [k]. ToString ()));

}

Return sb. ToString ();

}

 

Private static XmlDocument ReadXmlResponse (WebResponse response)

{

StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );

String retXml = sr. ReadToEnd ();

Sr. Close ();

XmlDocument doc = new XmlDocument ();

Doc. LoadXml (retXml );

Return doc;

}

 

Private static void AddDelaration (XmlDocument doc)

{

XmlDeclaration decl = doc. CreateXmlDeclaration ("1.0", "UTF-8", null );

Doc. InsertBefore (decl, doc. DocumentElement );

}

 

Private static Hashtable _ xmlNamespaces = new Hashtable (); // cache xmlNamespace to avoid repeated calls to GetNamespace

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Call and read the resolution returned results

 

DataSet ds = new DataSet ();

XmlNode xmlNode1;

XmlDataDocument xd = new XmlDataDocument ();

StringBuilder sb;

Hashtable ht = new Hashtable ();

Ht. Add ("xmlIn", "<Request> <MemCode> 001 </MemCode> </Request> ");

XmlNode1 = Hishop. Plugins. WebServiceCaller. QuerySoapWebService ("http://xxx.xxxx.com/Service.asmx", "SinPointQuery", ht );

If (xmlNode1 = null)

{

Return;

}

String xmlstr = HttpUtility. HtmlDecode (xmlNode1.OuterXml );

Sb = new StringBuilder (xmlstr );

If (sb. ToString (). Equals (""))

{

Return;

}

Xd. LoadXml (sb. ToString ());

Ds. ReadXml (new XmlNodeReader (xd ));

// Ds can return the result set.

The above is all the content of this article. I hope you will like it.

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.