1 |
|
/* |
2 |
|
* DotGNU XmlRpcClientProtocol implementation |
3 |
|
* |
4 |
|
* Copyright (C) 2002 netFluid Technology Ltd |
5 |
|
* Copyright (C) 2003 Adam Ballai |
6 |
|
* |
7 |
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
* it under the terms of the GNU General Public License as published by |
9 |
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
* (at your option) any later version. |
11 |
|
* |
12 |
|
* This program is distributed in the hope that it will be useful, |
13 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
* GNU General Public License for more details. |
16 |
|
* |
17 |
|
* You should have received a copy of the GNU General Public License |
18 |
|
* along with this program; if not, write to the Free Software |
19 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
* |
21 |
|
* $Revision$ $Date$ |
22 |
|
* |
23 |
|
* -------------------------------------------------------------------------- |
24 |
|
*/ |
25 |
|
namespace DotGNU.XmlRpc |
26 |
|
{ |
27 |
|
using System; |
28 |
|
using System.IO; |
29 |
|
using System.Text; |
30 |
|
using System.Net; |
31 |
|
using System.Net.Sockets; |
32 |
|
using System.Web.Services.Protocols; |
33 |
|
using System.Text.RegularExpressions; |
34 |
|
using DotGNU.XmlRpc.Serialization.Formatters; |
35 |
|
|
36 |
|
public class XmlRpcClientProtocol |
37 |
|
{ |
38 |
|
private String header; |
39 |
|
private Stream ns; |
40 |
|
private String dest; |
41 |
|
public String Url; |
42 |
|
bool connected = false; |
43 |
|
|
44 |
|
public XmlRpcClientProtocol() |
45 |
|
{ |
46 |
|
} |
47 |
|
|
48 |
|
private void connect() |
49 |
|
{ |
50 |
|
String url = String.Empty; |
51 |
|
String port = String.Empty; |
52 |
|
|
53 |
|
dest = String.Empty; |
54 |
|
|
55 |
|
String tmpUrl = Url; |
56 |
|
int spot = tmpUrl.LastIndexOf("://")+3; |
57 |
|
tmpUrl = tmpUrl.Substring(spot, tmpUrl.Length - spot); |
58 |
|
spot = tmpUrl.IndexOf("/"); |
59 |
|
dest = tmpUrl.Substring(spot, tmpUrl.Length-spot); |
60 |
|
tmpUrl = tmpUrl.Substring(0, spot); |
61 |
|
spot = tmpUrl.IndexOf(":"); |
62 |
|
if(spot != -1) |
63 |
|
{ |
64 |
|
// find port |
65 |
|
url = tmpUrl.Substring(0, spot); |
66 |
|
port = tmpUrl.Substring(spot+1, tmpUrl.Length -spot -1); |
67 |
|
} |
68 |
|
else |
69 |
|
{ |
70 |
|
port = "80"; |
71 |
|
url = tmpUrl; |
72 |
|
} |
73 |
|
IPHostEntry entry = Dns.Resolve(url); |
74 |
|
int porti = Convert.ToInt32(port); |
75 |
|
IPEndPoint ep = new IPEndPoint(entry.AddressList[0], porti ); |
76 |
|
|
77 |
|
// Connect to the remote server. |
78 |
|
Socket socket = new Socket(AddressFamily.InterNetwork, |
79 |
|
SocketType.Stream, |
80 |
|
ProtocolType.Unspecified); |
81 |
|
socket.Connect(ep); |
82 |
|
|
83 |
|
ns = new NetworkStream( socket ); |
84 |
|
|
85 |
|
connected = true; |
86 |
|
} |
87 |
|
|
88 |
|
public Object[] Invoke(String method_name, Object[] args ) |
89 |
|
{ |
90 |
|
if( !connected ) connect(); |
91 |
|
|
92 |
|
MemoryStream ms = new MemoryStream(); |
93 |
|
MethodCallFormatter call = new MethodCallFormatter(); |
94 |
|
XmlRpcMethod method = new XmlRpcMethod( method_name ); |
95 |
|
|
96 |
|
foreach( object obj in args) { |
97 |
|
method.Add( obj ); |
98 |
|
} |
99 |
|
|
100 |
|
call.Serialize( ms, method ); |
101 |
|
ms.Seek(0,0); |
102 |
|
|
103 |
|
// Got stream to send (ms) |
104 |
|
|
105 |
|
String header = "POST " + dest + " HTTP/1.0\nContent-Type: text/xml\nContent-Length: " + ms.Length.ToString() + "\r\n\r\n"; |
106 |
|
|
107 |
|
StreamWriter hw = new StreamWriter( ns ); |
108 |
|
|
109 |
|
hw.Write( header ); |
110 |
|
hw.Flush(); |
111 |
|
|
112 |
|
int len = 1024; |
113 |
|
int bread; |
114 |
|
byte[] buffer = new byte[len]; |
115 |
|
while( (bread = ms.Read(buffer, 0, len)) > 0) { |
116 |
|
//String s = Encoding.ASCII.GetString(buffer, 0, bread ); |
117 |
|
//Console.Write( "{0}", s ); |
118 |
|
ns.Write(buffer, 0, bread); |
119 |
|
} |
120 |
|
|
121 |
|
//while( (bread = ns.Read(buffer, 0, len)) > 0) { |
122 |
|
//String s = Encoding.ASCII.GetString(buffer, 0, bread ); |
123 |
|
//Console.Write( "{0}", s ); |
124 |
|
// } |
125 |
|
|
126 |
|
// Get Response |
127 |
|
MethodResponseFormatter response = new MethodResponseFormatter(); |
128 |
|
XmlRpcResponse r = (XmlRpcResponse)response.Deserialize( ns ); |
129 |
|
|
130 |
|
//Console.WriteLine( "Hmm: {0}", r.ToString() ); |
131 |
|
return r.ToArray(); |
132 |
|
} |
133 |
|
} |
134 |
|
} |