web analytics

Home of my tech rants, free programs, and a story or two…

C# Examples

Upload File Example

try {
	OpenFileDialog.Title = "Browse for file to upload...";
	OpenFileDialog.FileName = null;
	OpenFileDialog.Filter = "Image Files (JPEG, PNG)|*.png;*.jpg;*.jpeg";

	if (OpenFileDialog.ShowDialog() == DialogResult.OK) {
		string strHTTPResponseHeaders = null;
		string strServerResponse = null;

		Tom.httpHelper httpHelper = new Tom.httpHelper();
		httpHelper.useHTTPCompression(true);
		httpHelper.setProxyMode(true);
		httpHelper.setHTTPTimeout(10);
		httpHelper.setUserAgent("Microsoft .NET");
		// Set our User Agent String.
		httpHelper.addHTTPCookie("mycookie", "my cookie contents", "www.toms-world.org", "/");
		httpHelper.addHTTPHeader("myheader", "my header contents");
		httpHelper.addPOSTData("test1", "value1");
		httpHelper.addPOSTData("test2", "value2");
		httpHelper.addGETData("test3", "value3");
		httpHelper.addFileUpload("myfileupload", OpenFileDialog.FileName, null, null);

		if (httpHelper.uploadData("http://www.toms-world.org/httphelper.php", strServerResponse)) {
			strHTTPResponseHeaders = httpHelper.getHTTPResponseHeaders().ToString;
		}
	}
} catch (Tom.httpProtocolException ex) {
// You can handle httpProtocolExceptions different than normal exceptions with this code.
} catch (Net.WebException ex) {
	Interaction.MsgBox(ex.Message + " " + ex.StackTrace);
}

Post Data Example

try {
	string strHTTPResponseHeaders = null;
	string strServerResponse = null;

	Tom.httpHelper httpHelper = new Tom.httpHelper();
	httpHelper.useHTTPCompression(true);
	httpHelper.setProxyMode(true);
	httpHelper.setUserAgent("Microsoft .NET");
	// Set our User Agent String.
	httpHelper.addHTTPCookie("mycookie", "my cookie contents", "www.toms-world.org", "/");
	httpHelper.addHTTPHeader("myheader", "my header contents");
	httpHelper.addPOSTData("test1", "value1");
	httpHelper.addPOSTData("test2", "value2");
	httpHelper.addGETData("test3", "value3");
	httpHelper.addPOSTData("major", "3");
	httpHelper.addPOSTData("minor", "9");
	httpHelper.addPOSTData("build", "6");

	if (httpHelper.getWebData("http://www.toms-world.org/httphelper.php", strServerResponse)) {
		strHTTPResponseHeaders = httpHelper.getHTTPResponseHeaders().ToString;
	}
} catch (Tom.httpProtocolException ex) {
// You can handle httpProtocolExceptions different than normal exceptions with this code.
} catch (Net.WebException ex) {
	Interaction.MsgBox(ex.Message + " " + ex.StackTrace);
}

Get Web Data Example

try {
	string strHTTPResponseHeaders = null;
	string strServerResponse = null;

	Tom.httpHelper httpHelper = new Tom.httpHelper();
	httpHelper.useHTTPCompression(true);
	httpHelper.setProxyMode(true);
	httpHelper.setUserAgent("Microsoft .NET");
	// Set our User Agent String.
	httpHelper.addGETData("test3", "value3");
	httpHelper.addHTTPCookie("mycookie", "my cookie contents", "www.toms-world.org", "/");
	httpHelper.addHTTPHeader("myheader", "my header contents");

	if (httpHelper.getWebData("http://www.toms-world.org/httphelper.php", strServerResponse)) {
		strHTTPResponseHeaders = httpHelper.getHTTPResponseHeaders(true).ToString;
	}
} catch (Tom.httpProtocolException ex) {
// You can handle httpProtocolExceptions different than normal exceptions with this code.
} catch (Net.WebException ex) {
// You can handle web exceptions different than normal exceptions with this code.
} catch (Exception ex) {
	Interaction.MsgBox(ex.Message + " " + ex.StackTrace);
}

Setting a URL Pre-Processor

httpHelper httpHelper = new httpHelper();
httpHelper.setUserAgent("Microsoft .NET");
// Set our User Agent String.
httpHelper.useHTTPCompression(true);
httpHelper.setProxyMode(true);

httpHelper.setURLPreProcessor1 {
		strURLInput = "http://" + strURLInput;
	}

	return strURLInput;
});

File Download Example using an Injected Updating Function

private Threading.Thread downloadThread;
private void btnDownloadFile_Click(object sender, EventArgs e)
{
	// First we create our httpHelper Class instance.
	Tom.httpHelper httpHelper = new Tom.httpHelper();
	httpHelper.setUserAgent("Microsoft .NET");
	// Set our User Agent String.

	// Now we set up our download status updating Lambda function that's passed to the Class instance to execute within the memory space of the Class.
	httpHelper.setDownloadStatusUpdateRoutine2;
		Label2.Text = percentage.ToString + "%";
		ProgressBar1.Value = percentage;
		// Set the progress bar the percentage value.
	});

	// Now we need to create our download thread.
	downloadThread = new Threading.Thread3
				IO.File.Delete(pathToDownloadFileTo);
			Interaction.MsgBox("Download aborted.");
			// And tell the user that the download is aborted.
		}
	});
	downloadThread.Start();
	// Starts our download thread.
}

Setting a Custom Error Handler

Tom.httpHelper httpHelper = new Tom.httpHelper();

// The "ex" is the Exception Object, you can find out what happened in the class instance by referencing this object.
// The "classInstance" object is the current class instance that the customErrorHandler is running in. The class passes itself to your custom error handler so you can see what's going on inside the class and call any of the public properties and functions of the class.
httpHelper.setCustomErrorHandler((Exception ex, httpHelper classInstance) =>
{
	if (ex is Net.WebException | ex is httpProtocolException) {
		Interaction.MsgBox("The server responded with an HTTP error.", MsgBoxStyle.Critical, "Dialog Title");
	} else if (ex is sslErrorException) {
		Interaction.MsgBox("An HTTP SSL error occurred.", MsgBoxStyle.Critical, "Dialog Title");
	} else {
		Interaction.MsgBox("A general error occured.", MsgBoxStyle.Critical, "Dialog Title");
	}

	// For instance, you can call the toString() function using the classInstance object.
	classInstance.toString();
	// This outputs everything that the Class instance has in memory.
});
  1. string strURLInput) => { if (!strURLInput.StartsWith("http://", StringComparison.OrdinalIgnoreCase[]
  2. long remoteFileSize, long amountDownloaded, short percentage) => { Label1.Text = string.Format("Downloaded {0} of {1}", httpHelper.fileSizeToHumanReadableFormat(amountDownloaded), httpHelper.fileSizeToHumanReadableFormat(remoteFileSize[]
  3. ) => { string urlToFileToBeDownloaded = "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso"; string pathToDownloadFileTo = "S:\\ubuntu-16.04-desktop-amd64.iso"; try { btnStopDownload.Enabled = true; btnDownloadFile.Enabled = false; // We use the downloadFile() function which first calls for the URL and then the path to a place on the local file system to save it. This function is why we need multithreading, this will take a long time to do. httpHelper.downloadFile(urlToFileToBeDownloaded, pathToDownloadFileTo); btnDownloadFile.Enabled = true; btnStopDownload.Enabled = false; Interaction.MsgBox("Download complete."); // And tell the user that the download is complete. } catch (Net.WebException ex) { btnDownloadFile.Enabled = true; btnStopDownload.Enabled = false; Interaction.MsgBox(ex.Message + " " + ex.StackTrace); } catch (Threading.ThreadAbortException ex) { btnDownloadFile.Enabled = true; btnStopDownload.Enabled = false; if (IO.File.Exists(pathToDownloadFileTo[]