HttpWebRequest CreateWebRequest(string method, string objectKey, string queryString)
{
var uriString = new StringBuilder(Service.UseSsl ? "https://" : "http://");
if (bucketName != null && Service.UseSubdomains)
uriString.Append(bucketName).Append('.');
uriString.Append(Service.Host);
if (Service.CustomPort != 0)
uriString.Append(':').Append(Service.CustomPort);
uriString.Append('/');
if (bucketName != null && !Service.UseSubdomains)
uriString.Append(bucketName).Append('/');
// EscapeDataString allows you to use basically any key for an object, including
// keys with tricky URI characters like "+".
if (objectKey != null)
uriString.Append(Uri.EscapeDataString(objectKey));
if (queryString != null)
uriString.Append(queryString);
var uri = new Uri(uriString.ToString());
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(uri);
request.Method = method;
request.AllowWriteStreamBuffering = true; // AddObject will make this false
request.AllowAutoRedirect = true;
// S3 will never "timeout" a request. However, network delays may still cause a
// timeout according to WebRequest's ReadWriteTimeout property, which you can modify.
request.Timeout = int.MaxValue;
return request;
}