My favorites | Sign in
Project Logo
                
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Web;
using System.Threading;
using System.Text.RegularExpressions;

namespace Exyus.Web
{
public class HTTPClient
{
// public properties
public WebHeaderCollection RequestHeaders = new WebHeaderCollection();
public WebHeaderCollection ResponseHeaders = new WebHeaderCollection();
public CookieContainer CookieCollection = new CookieContainer();
public HttpStatusCode ResponseStatusCode = HttpStatusCode.OK;
public string ResponseDescription = string.Empty;
public DateTime ResponseLastModified = System.DateTime.MaxValue;
public NetworkCredential Credentials = new NetworkCredential();
public long ResponseLength = 0;
public string UserAgent = string.Empty;

public HTTPClient() { }
public HTTPClient(NetworkCredential credentials)
{
this.Credentials = credentials;
}
public HTTPClient(string user, string password)
{
this.Credentials = new NetworkCredential(user, password);
}
// method that makes the call
public string Execute(string url)
{
return Execute(url, Constants.HttpGet, Constants.cType_Xml, string.Empty);
}
public string Execute(string url, string method)
{
return Execute(url, method, Constants.cType_Xml, string.Empty);
}
public string Execute(string url, string method, string contentType)
{
return Execute(url, method, contentType, string.Empty);
}
public string Execute(string url, string method, string contentType, string body)
{
HttpWebRequest req = null;
HttpWebResponse resp= null;
string rtnBody = string.Empty;

try
{
// build request object
req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = (this.UserAgent.Length!=0?this.UserAgent:Constants.msc_exyus_agent);
req.Method = method.ToUpper();
req.ContentType = contentType;
req.Accept = contentType;
req.ContentLength = body.Length;
req.PreAuthenticate = true;
if (this.Credentials.UserName != string.Empty)
req.Credentials = this.Credentials;

// set headers
if (this.RequestHeaders != null)
{
for (int i = 0; i < this.RequestHeaders.Count; i++)
{
// some headers must be set as properties only
string key = this.RequestHeaders.GetKey(i);
string value = this.RequestHeaders[i];
switch (key.ToLower())
{
case "user-agent":
req.UserAgent = value;
break;
case "if-modified-since":
req.IfModifiedSince = DateTime.Parse(value);
break;
case "accept":
req.Accept = value;
break;
default:
req.Headers.Set(key, value);
break;
}
}
}

// set cookies
if (this.CookieCollection != null)
req.CookieContainer = this.CookieCollection;
if (HttpContext.Current != null &&
HttpContext.Current.Request != null &&
HttpContext.Current.Request.Headers != null &&
HttpContext.Current.Request.Headers[Constants.hdr_cookie] != null
)
req.CookieContainer.SetCookies(new Uri(url), HttpContext.Current.Request.Headers[Constants.hdr_cookie]);

// set body
if (body != null && body.Trim() != string.Empty)
{
using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
{
sw.Write(body);
sw.Close();
}
}

// now use request obj to populate response obj
resp = (HttpWebResponse)req.GetResponse();

// get properties
this.ResponseLength = resp.ContentLength;
this.ResponseStatusCode = resp.StatusCode;
this.ResponseDescription = resp.StatusDescription;
this.ResponseLastModified = resp.LastModified;

// get headers
this.ResponseHeaders = resp.Headers;

// get cookies
this.CookieCollection = new CookieContainer();
foreach (Cookie ck in resp.Cookies)
this.CookieCollection.Add(ck);

// get body
if (resp.ContentLength != 0)
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
rtnBody = sr.ReadToEnd();
sr.Close();
}
}

// clean up
if (resp != null)
resp.Close();

resp = null;
req = null;

// return the results (if any)
return rtnBody;
}
catch (HttpException hex)
{
throw new HttpException(hex.GetHttpCode(), hex.Message);
}
catch (WebException wex)
{
// typical http error
if (wex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse wrsp = (HttpWebResponse)wex.Response;
throw new HttpException((int)wrsp.StatusCode, wrsp.StatusDescription);
}
else
{
throw new HttpException(500, wex.Message);
}
}
catch (Exception ex)
{
throw new HttpException(500, ex.Message);
}
}
}

// used to invalidate a collection of URIs
public class CollectionRequestor
{
public string[] Uri;
public CookieContainer CookieCollection = new CookieContainer();
public string defaultType;
public Hashtable UriTypeMap;
public bool NoCache = true;

public void Execute()
{
Utility util = new Utility();
HTTPClient cl = new HTTPClient();
cl.UserAgent = Constants.msc_exyus_cachebot;

if (this.NoCache == true)
{
cl.RequestHeaders.Set(Constants.hdr_cache_control, "no-cache");
}
cl.Credentials = util.GetSystemCredentials();
cl.CookieCollection = this.CookieCollection;

for (int i = 0; i < Uri.Length; i++)
{
string[] media = util.GetResourceMediaTypes(Uri[i], this.defaultType, this.UriTypeMap);
if (media != null && media.Length != 0)
{
for (int j = 0; j < media.Length; j++)
{
cl.RequestHeaders.Set("Accept", media[j]);
try {cl.Execute(this.Uri[i], "head", media[j]);}
catch (Exception ex) { }
}
}
else
{
try {cl.Execute(this.Uri[i], "head");}
catch (Exception ex) { }
}
}
}
}

}
Show details Hide details

Change log

r170 by m...@amundsen.com on Apr 03, 2008   Diff
Cache.cs
  * added additional checks for work
around IE Mobile bug that sends garbage
for last-modified-date
HTTPClient.cs
  * added "exyusCacheBot" as default user
agent in CollectionRequestor class
Go to: 
Project members, sign in to write a code review

Older revisions

r137 by m...@amundsen.com on Feb 27, 2008   Diff
HTTPClient.cs
  * fixed spelling error in local
argument name
  * added NoCache public property to
CollectionRequestor class
r108 by m...@amundsen.com on Jan 29, 2008   Diff
HTTPClient.cs
  * added public UserAgent property
  * added code to handle
RequestHeaders.Add("user-agent","my
agent")
r58 by m...@amundsen.com on Jan 16, 2008   Diff
HTTPClient.cs
  * removed useless call to Utility()
XmlFileResource.cs
  * re-ored var names at top of class
  * changed _get-condition passed XSLT
...
All revisions of this file

File info

Size: 8074 bytes, 219 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Id Rev Date Author
Hosted by Google Code