123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using COSXML.Auth;
- using COSXML.Log;
- using COSXML.Common;
- using System.Net;
- namespace COSXML.Network
- {
- public sealed class Request
- {
- private static string TAG = "Request";
- // post put get delete head, etc.
- private string method;
- // shceme://host:port/path?query# etc.
- private HttpUrl url;
- // key : value
- private Dictionary<string, string> headers;
- // file or byte, etc.
- private RequestBody body;
- // package tag for request
- private Object tag;
- private bool isHttps;
- private string userAgent;
- private string host;
- private string urlString;
- private HttpWebRequest realeHttpRequest;
- public Request()
- {
- headers = new Dictionary<string, string>();
- this.onNotifyGetResponse = this.HandleGetResponse;
- }
- public string Method
- {
- get
- {
- return method;
- }
- set { method = value; }
- }
- public bool IsHttps
- {
- get
- {
- return isHttps;
- }
- set { isHttps = value; }
- }
- public string UserAgent
- {
- get
- {
- return userAgent == null ? CosVersion.GetUserAgent() : userAgent;
- }
- set { userAgent = value; }
- }
- public string Host
- {
- get
- {
- return host == null ? url.Host : host;
- }
- set { host = value; }
- }
- public HttpUrl Url
- {
- get
- {
- //if (url == null) throw new ArgumentNullException("httpUrl == null");
- return url;
- }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("httpUrl == null");
- }
- url = value;
- }
- }
- public string RequestUrlString
- {
- get
- {
- if (urlString == null)
- {
- urlString = url.ToString();
- }
- return urlString;
- }
- set { urlString = value; }
- }
- public Dictionary<string, string> Headers
- {
- get
- {
- return headers;
- }
-
- private set { }
- }
- public void AddHeader(string name, string value)
- {
- try
- {
- headers.Add(name, value);
- }
- catch (ArgumentNullException)
- {
- QLog.Debug(TAG, "AddHeader: name is null");
- }
- catch (ArgumentException)
- {
- if (String.IsNullOrEmpty(value))
- {
- return;
- }
- if (!String.IsNullOrEmpty(headers[name]))
- {
- headers[name] = headers[name] + ',' + value;
- }
- else
- {
- headers[name] = value;
- }
- }
- }
- public RequestBody Body
- {
- get
- {
- return body;
- }
- set { body = value; }
- }
- public COSXML.Callback.OnNotifyGetResponse onNotifyGetResponse;
- private void HandleGetResponse()
- {
- if (body != null)
- {
- body.OnNotifyGetResponse();
- }
- }
- public void BindHttpWebRequest(HttpWebRequest httpWebRequest)
- {
- this.realeHttpRequest = httpWebRequest;
- }
- public void Cancel()
- {
- if (realeHttpRequest != null)
- {
- realeHttpRequest.Abort();
- }
- }
- }
- }
|