~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/server/NU-Simias.Dav/DavClient/.svn/text-base/Request.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***********************************************************************
2
 
 *  $RCSfile: Request.cs,v $
3
 
 * 
4
 
 *  Copyright (C) 2005 Novell, Inc.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 *
20
 
 *  Author: Brady Anderson (banderso@novell.com)
21
 
 * 
22
 
 ***********************************************************************/
23
 
using System;
24
 
using System.Collections;
25
 
using System.IO;
26
 
using System.Net;
27
 
using System.Text;
28
 
using System.Web;
29
 
using System.Web.SessionState;
30
 
 
31
 
namespace Novell.DavClient
32
 
{
33
 
        public class Request
34
 
        {
35
 
                private class HttpHeader
36
 
                {
37
 
                        public string Name;
38
 
                        public string Value;
39
 
                }
40
 
                
41
 
                #region Class Members
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\"";
53
 
                #endregion
54
 
        
55
 
                #region Properties
56
 
 
57
 
                public string Method
58
 
                {
59
 
                        get{ return method; }
60
 
                        set{ method = value.ToUpper(); }
61
 
                }
62
 
                
63
 
                public string Resource
64
 
                {
65
 
                        get{ return resource; }
66
 
                        set{ resource = value; }
67
 
                }
68
 
                
69
 
                public HttpStatusCode ResponseStatus
70
 
                {
71
 
                        get
72
 
                        {
73
 
                                if ( response != null )
74
 
                                {
75
 
                                        return status;
76
 
                                }
77
 
                                
78
 
                                
79
 
                                // throw exception
80
 
                                return 0;
81
 
                        }
82
 
                }
83
 
                
84
 
                #endregion
85
 
                
86
 
                #region Constructors
87
 
                public Request( WebState State )
88
 
                {
89
 
                        webState = State;
90
 
                        headers = new ArrayList();
91
 
                        method = "POST";
92
 
                }
93
 
                #endregion
94
 
        
95
 
        
96
 
                public void AddHeader( string Name, string Value )
97
 
                {
98
 
                        HttpHeader header = new HttpHeader();
99
 
                        header.Name = Name;
100
 
                        header.Value = Value;
101
 
                        
102
 
                        headers.Add( header);
103
 
                }
104
 
 
105
 
                public void RemoveHeader( string Name )
106
 
                {
107
 
                        return;
108
 
                }
109
 
                
110
 
                public string GetResponseHeader( string Name )
111
 
                {
112
 
                        if ( response != null )
113
 
                        {
114
 
                                return response.GetResponseHeader( Name );
115
 
                        }
116
 
                        
117
 
                        return null;
118
 
                }
119
 
                
120
 
                public void SetBodyContent( string content )
121
 
                {
122
 
                        contentLength = content.Length;
123
 
                        this.content = content;
124
 
                }
125
 
                
126
 
                public virtual void Send()
127
 
                {
128
 
                        if ( resource != null )
129
 
                        {
130
 
                                Uri requestUri = new Uri( webState.ServiceUrl + resource );
131
 
                                Console.WriteLine( "request URI: " + requestUri.ToString() );
132
 
                                request = WebRequest.Create( requestUri ) as HttpWebRequest;
133
 
                        }
134
 
                        else
135
 
                        {
136
 
                                request = WebRequest.Create( webState.ServiceUrl ) as HttpWebRequest;
137
 
                        }
138
 
                                
139
 
                        webState.SetRequestState( request );
140
 
                        request.ContentType = contentType;
141
 
                        
142
 
                        foreach( HttpHeader header in headers )
143
 
                        {
144
 
                                request.Headers.Add( header.Name, header.Value ); 
145
 
                        }
146
 
                        
147
 
                        request.Method = method;
148
 
                        request.ProtocolVersion = HttpVersion.Version11;
149
 
                        request.ContentLength = contentLength;
150
 
                        
151
 
                        try
152
 
                        {
153
 
                                if ( contentLength > 0 && content != "" )
154
 
                                {
155
 
                                        StreamWriter s = new StreamWriter( request.GetRequestStream(), Encoding.UTF8 );
156
 
                                        //StreamWriter s = new StreamWriter( request.GetRequestStream(), Encoding.ASCII );
157
 
                    s.Write( content );
158
 
                    s.Close();                          
159
 
                                }
160
 
                                request.GetRequestStream().Close();
161
 
                                
162
 
                                response = request.GetResponse() as HttpWebResponse;
163
 
                                if ( response != null )
164
 
                                {
165
 
                                        request.CookieContainer.Add( response.Cookies );
166
 
                                        
167
 
                                        status = HttpStatusCode.OK;
168
 
                                }
169
 
                        }
170
 
                        catch(WebException webEx)
171
 
                        {
172
 
                                status = (HttpStatusCode) webEx.Status;
173
 
                                Console.WriteLine( webEx.Status.ToString() );
174
 
                                Console.WriteLine( webEx.Message );
175
 
                        }
176
 
                        catch(Exception ex)
177
 
                        {
178
 
                                Console.WriteLine( ex.Message );
179
 
                                throw ex;
180
 
                        }
181
 
 
182
 
//                      return status;
183
 
                
184
 
                }
185
 
        
186
 
        }
187
 
}