#if NET_2_0
async = false;
#endif
- WebRequest request = SetupRequest (address, method);
+ WebRequest request = SetupRequest (address, method, true);
return request.GetRequestStream ();
} catch (Exception ex) {
throw new WebException ("An error occurred " +
}
}
- private string DetermineMethod (Uri address, string method)
+ private string DetermineMethod (Uri address, string method, bool is_upload)
{
if (method != null)
return method;
#if NET_2_0
if (address.Scheme == Uri.UriSchemeFtp)
- return "RETR";
+ return (is_upload) ? "STOR" : "RETR";
#endif
- return "POST";
+ return (is_upload) ? "POST" : "GET";
}
// UploadData
async = false;
#endif
return UploadDataCore (address, method, data, null);
+ } catch (WebException) {
+ throw;
} catch (Exception ex) {
throw new WebException ("An error occurred " +
"performing a WebClient request.", ex);
throw new ArgumentNullException ("data");
#endif
- WebRequest request = SetupRequest (address, method);
+ WebRequest request = SetupRequest (address, method, true);
try {
int contentLength = data.Length;
request.ContentLength = contentLength;
WebRequest request = null;
try {
fStream = File.OpenRead (fileName);
- request = SetupRequest (address, method);
+ request = SetupRequest (address, method, true);
reqStream = request.GetRequestStream ();
byte [] realBoundary = Encoding.ASCII.GetBytes ("--" + boundary + "\r\n");
reqStream.Write (realBoundary, 0, realBoundary.Length);
"value for this request.");
Headers ["Content-Type"] = urlEncodedCType;
- WebRequest request = SetupRequest (uri, method);
+ WebRequest request = SetupRequest (uri, method, true);
try {
Stream rqStream = request.GetRequestStream ();
MemoryStream tmpStream = new MemoryStream ();
#if NET_2_0
public string DownloadString (string address)
{
- return encoding.GetString (DownloadData (address));
+ if (address == null)
+ throw new ArgumentNullException ("address");
+
+ return encoding.GetString (DownloadData (CreateUri (address)));
}
public string DownloadString (Uri address)
{
- return encoding.GetString (DownloadData (address));
+ if (address == null)
+ throw new ArgumentNullException ("address");
+
+ return encoding.GetString (DownloadData (CreateUri (address)));
}
public string UploadString (string address, string data)
#endif
}
- Uri MakeUri (string path)
+#if NET_2_0
+ Uri CreateUri (Uri address)
{
- string query = null;
- if (queryString != null && queryString.Count != 0) {
- // This is not the same as UploadValues, because these 'keys' are not
- // urlencoded here.
- StringBuilder sb = new StringBuilder ();
+ string query = address.Query;
+ if (String.IsNullOrEmpty (query))
+ query = GetQueryString (true);
+
+ if (baseAddress == null && query == null)
+ return address;
+
+ if (baseAddress == null)
+ return new Uri (address.ToString () + query, (query != null));
+
+ if (query == null)
+ return new Uri (baseAddress, address.ToString ());
+
+ return new Uri (baseAddress, address.ToString () + query, (query != null));
+
+ }
+#endif
+
+ string GetQueryString (bool add_qmark)
+ {
+ if (queryString == null || queryString.Count == 0)
+ return null;
+
+ StringBuilder sb = new StringBuilder ();
+ if (add_qmark)
sb.Append ('?');
- foreach (string key in queryString)
- sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
- if (sb.Length != 0) {
- sb.Length--; // remove trailing '&'
- query = sb.ToString ();
- }
- }
+ foreach (string key in queryString)
+ sb.AppendFormat ("{0}={1}&", key, UrlEncode (queryString [key]));
+
+ if (sb.Length != 0)
+ sb.Length--; // removes last '&' or the '?' if empty.
+ if (sb.Length == 0)
+ return null;
+
+ return sb.ToString ();
+ }
+
+ Uri MakeUri (string path)
+ {
+ string query = GetQueryString (true);
if (baseAddress == null && query == null) {
try {
return new Uri (path);
return request;
}
- WebRequest SetupRequest (Uri uri, string method)
+ WebRequest SetupRequest (Uri uri, string method, bool is_upload)
{
WebRequest request = SetupRequest (uri);
- request.Method = DetermineMethod (uri, method);
+ request.Method = DetermineMethod (uri, method, is_upload);
return request;
}
object [] args = (object []) state;
WebRequest request = null;
try {
- request = SetupRequest ((Uri) args [0], (string) args [1]);
+ request = SetupRequest ((Uri) args [0], (string) args [1], true);
Stream stream = request.GetRequestStream ();
OnOpenWriteCompleted (
new OpenWriteCompletedEventArgs (stream, null, false, args [2]));