1
/***********************************************************************
2
* $RCSfile: Request.cs,v $
4
* Copyright (C) 2005 Novell, Inc.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public
17
* License along with this program; if not, write to the Free
18
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
* Author: Brady Anderson (banderso@novell.com)
22
***********************************************************************/
24
using System.Collections;
29
using System.Web.SessionState;
31
namespace Novell.DavClient
35
private class HttpHeader
42
private HttpWebResponse response = null;
43
private HttpWebRequest request = null;
44
private HttpStatusCode status = 0;
45
private int contentLength = 0;
46
private string content = "";
47
private string method = null;
48
private string resource = null;
49
private WebState webState = null;
50
private ArrayList headers;
51
static internal readonly string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
52
private readonly string contentType = "text/xml; charset=\"utf-8\"";
60
set{ method = value.ToUpper(); }
63
public string Resource
65
get{ return resource; }
66
set{ resource = value; }
69
public HttpStatusCode ResponseStatus
73
if ( response != null )
87
public Request( WebState State )
90
headers = new ArrayList();
96
public void AddHeader( string Name, string Value )
98
HttpHeader header = new HttpHeader();
100
header.Value = Value;
102
headers.Add( header);
105
public void RemoveHeader( string Name )
110
public string GetResponseHeader( string Name )
112
if ( response != null )
114
return response.GetResponseHeader( Name );
120
public void SetBodyContent( string content )
122
contentLength = content.Length;
123
this.content = content;
126
public virtual void Send()
128
if ( resource != null )
130
Uri requestUri = new Uri( webState.ServiceUrl + resource );
131
Console.WriteLine( "request URI: " + requestUri.ToString() );
132
request = WebRequest.Create( requestUri ) as HttpWebRequest;
136
request = WebRequest.Create( webState.ServiceUrl ) as HttpWebRequest;
139
webState.SetRequestState( request );
140
request.ContentType = contentType;
142
foreach( HttpHeader header in headers )
144
request.Headers.Add( header.Name, header.Value );
147
request.Method = method;
148
request.ProtocolVersion = HttpVersion.Version11;
149
request.ContentLength = contentLength;
153
if ( contentLength > 0 && content != "" )
155
StreamWriter s = new StreamWriter( request.GetRequestStream(), Encoding.UTF8 );
156
//StreamWriter s = new StreamWriter( request.GetRequestStream(), Encoding.ASCII );
160
request.GetRequestStream().Close();
162
response = request.GetResponse() as HttpWebResponse;
163
if ( response != null )
165
request.CookieContainer.Add( response.Cookies );
167
status = HttpStatusCode.OK;
170
catch(WebException webEx)
172
status = (HttpStatusCode) webEx.Status;
173
Console.WriteLine( webEx.Status.ToString() );
174
Console.WriteLine( webEx.Message );
178
Console.WriteLine( ex.Message );