if (this.DumpHeaders)
{
Console.WriteLine(String.Format("{0} {1} HTTP/{2}", webReq.Method, webReq.RequestUri.AbsoluteUri, webReq.ProtocolVersion));
DumpHeaderCollectionToConsole(webReq.Headers);
}
//Console.WriteLine(String.Format("ThreadID: {2} Requesting {0} on behalf of client {1}", webReq.RequestUri, client.Client.RemoteEndPoint.ToString(), Thread.CurrentThread.ManagedThreadId));
webReq.Timeout = 15000;
try
{
response = (HttpWebResponse)webReq.GetResponse();
}
catch (WebException webEx)
{
response = webEx.Response as HttpWebResponse;
}
if (response != null)
{
List<Tuple<String,String>> responseHeaders = ProcessResponse(response);
StreamWriter myResponseWriter = new StreamWriter(outStream);
Stream responseStream = response.GetResponseStream();
try
{
//send the response status and response headers
WriteResponseStatus(response.StatusCode, response.StatusDescription, myResponseWriter);
WriteResponseHeaders(myResponseWriter, responseHeaders);
Byte[] buffer;
if (response.ContentLength > 0)
buffer = new Byte[response.ContentLength];
else
buffer = new Byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, bytesRead);
if (this.DumpResponseData)
Console.Write(UTF8Encoding.UTF8.GetString(buffer, 0, bytesRead));
}
if (this.DumpResponseData)
{
Console.WriteLine();
Console.WriteLine();
}
responseStream.Close();
outStream.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
responseStream.Close();
response.Close();
myResponseWriter.Close();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (this.DumpHeaders || this.DumpPostData || this.DumpResponseData)
{
//release the lock
Monitor.Exit(_outputLockObj);
}
clientStreamReader.Close();
clientStream.Close();
outStream.Close();
}
}
private static List<Tuple<String, String>> ProcessResponse(HttpWebResponse response)
{
String value=null;
String header=null;
List<Tuple<String, String>> returnHeaders = new List<Tuple<String, String>>();
foreach (String s in response.Headers.Keys)
{
if (s.ToLower() == "set-cookie")
{
header = s;
value = response.Headers;
}
else
returnHeaders.Add(new Tuple<String, String>(s, response.Headers));
}
if (!String.IsNullOrWhiteSpace(value))
{
response.Headers.Remove(header);
String[] cookies = cookieSplitRegEx.Split(value);
foreach (String cookie in cookies)
returnHeaders.Add(new Tuple<String, String>("Set-Cookie", cookie));
}
//returnHeaders.Add(new Tuple<String, String>("X-Proxied-By", "esri-application proxy"));
return returnHeaders;
}
private void WriteResponseStatus(HttpStatusCode code, String description, StreamWriter myResponseWriter)
{
String s = String.Format("HTTP/1.0 {0} {1}", (Int32)code, description);
myResponseWriter.WriteLine(s);
if (this.DumpHeaders)
Console.WriteLine(s);
}
private void WriteResponseHeaders(StreamWriter myResponseWriter, List<Tuple<String, String>> headers)
{
if (headers != null)
{
foreach (Tuple<String,String> header in headers)
myResponseWriter.WriteLine(String.Format("{0}: {1}", header.Item1, header.Item2));
}
myResponseWriter.WriteLine();
myResponseWriter.Flush();
if (this.DumpHeaders)
DumpHeaderCollectionToConsole(headers);
}
private static void DumpHeaderCollectionToConsole(WebHeaderCollection headers)
{
foreach (String s in headers.AllKeys)
Console.WriteLine(String.Format("{0}: {1}", s, headers));
Console.WriteLine();
}
private static void DumpHeaderCollectionToConsole(List<Tuple<String, String>> headers)
{
foreach (Tuple<String,String> header in headers)
Console.WriteLine(String.Format("{0}: {1}", header.Item1, header.Item2));
Console.WriteLine();
}
private static int ReadRequestHeaders(StreamReader sr, HttpWebRequest webReq)
{
String httpCmd;
int contentLen = 0;
do
{
httpCmd = sr.ReadLine();
if (String.IsNullOrEmpty(httpCmd))
return contentLen;
String[] header = httpCmd.Split(colonSpaceSplit, 2, StringSplitOptions.None);
switch (header[0].ToLower())
{
case "host":
// since HttpWebRequests correctly add the "Host" property anyway, and the
// requests from the ArcGIS WPF API change the "Host" to the ProxyURL, we
// don't want to set that here. It will just end in tears, I know it.
//webReq.Host = header[1];
break;
case "user-agent":
webReq.UserAgent = header[1];
break;
case "accept":
webReq.Accept = header[1];
break;
case "referer":
webReq.Referer = header[1];
break;
case "cookie":
webReq.Headers["Cookie"] = header[1];
break;
case "proxy-connection":
case "connection":
case "keep-alive":
//ignore these
break;
case "content-length":
int.TryParse(header[1], out contentLen);
break;
case "content-type":
webReq.ContentType = header[1];
break;
case "if-modified-since":
String[] sb = header[1].Trim().Split(semiSplit);
DateTime d;
if (DateTime.TryParse(sb[0], out d))
webReq.IfModifiedSince = d;
break;
default:
try
{
webReq.Headers.Add(header[0], header[1]);
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Could not add header {0}. Exception message:{1}", header[0], ex.Message));
}
break;
}
} while (!String.IsNullOrWhiteSpace(httpCmd));
return contentLen;
}
}