using System;
using System.IO;
using System.Net;
public class HttpPut {
public static void Main(string [] args) {
string url = "http://www.kutayzorlu.com/java2s/com/";
string username = "user";
string password = "password";
string data = "This data should be written to the URL.";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.Credentials = new NetworkCredential(username, password);
request.ContentLength = data.Length;
request.ContentType = "text/plain";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) {
writer.WriteLine(data);
}
WebResponse response = request.GetResponse( );
using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) {
while (reader.Peek( ) != -1) {
Console.WriteLine(reader.ReadLine( ));
}
}
}
}
Month: February 2011
New Math Client
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class NewMathClient
{
public static int Main(string[] argv)
{
HttpChannel chan = new HttpChannel();
ChannelServices.RegisterChannel(chan);
MathClass obj = new MathClass();
if (obj == null)
System.Console.WriteLine("Could not locate server");
else
{
int a = Convert.ToInt32(argv[0]);
int b = Convert.ToInt32(argv[1]);
int c = obj.Add(a, b);
Console.WriteLine("a + b = {0}", c);
c = obj.Subtract(a, b);
Console.WriteLine("a - b = {0}", c);
c = obj.Multiply(a, b);
Console.WriteLine("a * b = {0}", c);
c = obj.Divide(a, b);
Console.WriteLine("a / b = {0}", c);
}
return 0;
}
}
public class MathClass : MarshalByRefObject
{
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int Subtract(int a, int b)
{
int c = a - b;
return c;
}
public int Multiply(int a, int b)
{
int c = a * b;
return c;
}
public int Divide(int a, int b)
{
int c;
if (b != 0)
c = a / b;
else
c = 0;
return c;
}
}
Math Client
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class MathClient
{
public static int Main(string[] argv)
{
HttpChannel chan = new HttpChannel();
ChannelServices.RegisterChannel(chan);
MathClass obj = (MathClass)Activator.GetObject(
typeof(MathClass), "http://127.0.0.1:9050/MyMathServer");
if (obj == null)
System.Console.WriteLine("Could not locate server");
else
{
int a = Convert.ToInt32(argv[0]);
int b = Convert.ToInt32(argv[1]);
int c = obj.Add(a, b);
Console.WriteLine("a + b = {0}", c);
c = obj.Subtract(a, b);
Console.WriteLine("a - b = {0}", c);
c = obj.Multiply(a, b);
Console.WriteLine("a * b = {0}", c);
c = obj.Divide(a, b);
Console.WriteLine("a / b = {0}", c);
}
return 0;
}
}
public class MathClass : MarshalByRefObject
{
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int Subtract(int a, int b)
{
int c = a - b;
return c;
}
public int Multiply(int a, int b)
{
int c = a * b;
return c;
}
public int Divide(int a, int b)
{
int c;
if (b != 0)
c = a / b;
else
c = 0;
return c;
}
}
HttpChannel Math Server
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class MathServer
{
public static int Main()
{
HttpChannel chan = new HttpChannel(9050);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(
Type.GetType("MathClass, MathClass"), "MyMathServer",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Hit <enter> to exit...");
Console.ReadLine();
return 0;
}
}
public class MathClass : MarshalByRefObject
{
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int Subtract(int a, int b)
{
int c = a - b;
return c;
}
public int Multiply(int a, int b)
{
int c = a * b;
return c;
}
public int Divide(int a, int b)
{
int c;
if (b != 0)
c = a / b;
else
c = 0;
return c;
}
}
FtpWebResponse GUI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.IO;
class FtpClientForm : Form {
public FtpClientForm() {
InitializeComponent();
}
private string serverDirectory;
private void OnOpen(object sender, EventArgs e) {
Cursor currentCursor = this.Cursor;
FtpWebResponse response = null;
Stream stream = null;
this.Cursor = Cursors.WaitCursor;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textServer.Text);
request.Credentials = new NetworkCredential(textUsername.Text,
textPassword.Text);
request.Method = WebRequestMethods.Ftp.ListDirectory;
response = (FtpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
FillDirectoryList(stream);
serverDirectory = null;
buttonOpenDirectory.Enabled = false;
buttonGetFile.Enabled = false;
if (response != null)
response.Close();
if (stream != null)
stream.Close();
this.Cursor = currentCursor;
}
private void FillDirectoryList(Stream stream) {
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
string[] files = content.Split('
');
listFiles.DataSource = files;
reader.Close();
}
private void OnOpenDirectory(object sender, EventArgs e) {
FtpWebResponse response = null;
Stream stream = null;
string subDirectory = listFiles.SelectedValue.ToString().Trim();
serverDirectory += @"/" + subDirectory;
Uri baseUri = new Uri(textServer.Text);
Uri uri = new Uri(baseUri, serverDirectory);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(textUsername.Text,
textPassword.Text);
request.Method = WebRequestMethods.Ftp.ListDirectory;
response = (FtpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
FillDirectoryList(stream);
if (response != null)
response.Close();
if (stream != null)
stream.Close();
}
private void OnDownloadFile(object sender, EventArgs e) {
FtpWebResponse response = null;
Stream inStream = null;
Stream outStream = null;
Uri baseUri = new Uri(textServer.Text);
string filename = listFiles.SelectedValue.ToString().Trim();
string fullFilename = serverDirectory + @"/" + filename;
Uri uri = new Uri(baseUri, fullFilename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(textUsername.Text,textPassword.Text);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = checkBoxBinary.Checked;
response = (FtpWebResponse)request.GetResponse();
inStream = response.GetResponseStream();
saveFileDialog1.FileName = filename;
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
outStream = File.OpenWrite(saveFileDialog1.FileName);
byte[] buffer = new byte[4096];
int size = 0;
while ((size = inStream.Read(buffer, 0, 4096)) > 0) {
outStream.Write(buffer, 0, size);
}
}
if (inStream != null)
inStream.Close();
if (outStream != null)
outStream.Close();
if (response != null)
response.Close();
}
private void OnFileSelection(object sender, EventArgs e) {
this.buttonGetFile.Enabled = true;
this.buttonOpenDirectory.Enabled = true;
}
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label();
this.textServer = new System.Windows.Forms.TextBox();
this.buttonOpen = new System.Windows.Forms.Button();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.listFiles = new System.Windows.Forms.ListBox();
this.buttonOpenDirectory = new System.Windows.Forms.Button();
this.buttonGetFile = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.checkBoxBinary = new System.Windows.Forms.CheckBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textUsername = new System.Windows.Forms.TextBox();
this.textPassword = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 31);
this.label1.Size = new System.Drawing.Size(37, 13);
this.label1.Text = "Server:";
this.textServer.Location = new System.Drawing.Point(109, 31);
this.textServer.Size = new System.Drawing.Size(146, 20);
this.textServer.Text = "ftp://";
this.buttonOpen.Location = new System.Drawing.Point(371, 31);
this.buttonOpen.Size = new System.Drawing.Size(103, 23);
this.buttonOpen.Text = "Open";
this.buttonOpen.Click += new System.EventHandler(this.OnOpen);
this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
this.statusStrip1.Location = new System.Drawing.Point(0, 0);
this.statusStrip1.Size = new System.Drawing.Size(496, 18);
this.statusStrip1.Text = "statusStrip1";
this.listFiles.FormattingEnabled = true;
this.listFiles.Location = new System.Drawing.Point(28, 149);
this.listFiles.Size = new System.Drawing.Size(315, 160);
this.listFiles.SelectedIndexChanged += new System.EventHandler(this.OnFileSelection);
this.buttonOpenDirectory.Enabled = false;
this.buttonOpenDirectory.Location = new System.Drawing.Point(371, 77);
this.buttonOpenDirectory.Name = "buttonOpenDirectory";
this.buttonOpenDirectory.Size = new System.Drawing.Size(103, 23);
this.buttonOpenDirectory.TabIndex = 8;
this.buttonOpenDirectory.Text = "Open Directory";
this.buttonOpenDirectory.Click += new System.EventHandler(this.OnOpenDirectory);
this.buttonGetFile.Enabled = false;
this.buttonGetFile.Location = new System.Drawing.Point(371, 122);
this.buttonGetFile.Size = new System.Drawing.Size(103, 23);
this.buttonGetFile.Text = "Get File";
this.buttonGetFile.Click += new System.EventHandler(this.OnDownloadFile);
this.checkBoxBinary.AutoSize = true;
this.checkBoxBinary.Checked = true;
this.checkBoxBinary.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxBinary.Location = new System.Drawing.Point(371, 190);
this.checkBoxBinary.Size = new System.Drawing.Size(81, 17);
this.checkBoxBinary.Text = "Binary Mode";
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 62);
this.label2.Size = new System.Drawing.Size(54, 13);
this.label2.Text = "Username:";
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 101);
this.label3.Size = new System.Drawing.Size(52, 13);
this.label3.Text = "Password:";
this.textUsername.Location = new System.Drawing.Point(109, 62);
this.textUsername.Size = new System.Drawing.Size(146, 20);
this.textUsername.Text = "Anonymous";
this.textPassword.Location = new System.Drawing.Point(109, 101);
this.textPassword.PasswordChar = '?';
this.textPassword.Size = new System.Drawing.Size(146, 20);
this.textPassword.UseSystemPasswordChar = true;
this.ClientSize = new System.Drawing.Size(496, 353);
this.Controls.Add(this.textPassword);
this.Controls.Add(this.textUsername);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.checkBoxBinary);
this.Controls.Add(this.buttonGetFile);
this.Controls.Add(this.buttonOpenDirectory);
this.Controls.Add(this.listFiles);
this.Controls.Add(this.buttonOpen);
this.Controls.Add(this.textServer);
this.Controls.Add(this.label1);
this.Text = "FTP Client";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textServer;
private System.Windows.Forms.Button buttonOpen;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ListBox listFiles;
private System.Windows.Forms.Button buttonOpenDirectory;
private System.Windows.Forms.Button buttonGetFile;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.CheckBox checkBoxBinary;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textUsername;
private System.Windows.Forms.TextBox textPassword;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new FtpClientForm());
}
}
Using DsmlSoapHttpConnection
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
public class MainClass {
public static void Main() {
Uri uri = new Uri("http://yourSite:8080/dsml3");
DsmlDirectoryIdentifier identifier = new DsmlDirectoryIdentifier(uri);
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = @"explorercnagel";
credentials.Password = "password";
DsmlSoapHttpConnection dsmlConnection = new DsmlSoapHttpConnection(identifier);
string baseDN = null;
string ldapSearchFilter = "(objectClass=*)";
string[] attributesToReturn = null;
SearchRequest searchRequest = new SearchRequest(baseDN, ldapSearchFilter,System.DirectoryServices.Protocols.SearchScope.Base, attributesToReturn);
SearchResponse searchResponse = (SearchResponse)dsmlConnection.SendRequest(searchRequest);
foreach (SearchResultEntry entry in searchResponse.Entries) {
DirectoryAttribute attribute = entry.Attributes["schemaNamingContext"];
Console.WriteLine(attribute.Name + "=" + attribute[0]);
foreach (DirectoryAttribute attr in entry.Attributes.Values) {
Console.Write(attr.Name + "=");
foreach (object value in attr) {
Console.Write(value + " ");
}
}
}
}
}
Using DsmlDirectoryIdentifier
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
public class MainClass {
public static void Main() {
Uri uri = new Uri("http://yourSite/dsml1");
DsmlDirectoryIdentifier identifier = new DsmlDirectoryIdentifier(uri);
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "yourName";
credentials.Password = "password";
DsmlSoapHttpConnection connection = new DsmlSoapHttpConnection(identifier, credentials);
connection.BeginSession();
string distinguishedname = "yourName";
string ldapFilter = "object=user";
string[] attributes = new string[1];
DirectoryRequest request = new SearchRequest(distinguishedname, ldapFilter, System.DirectoryServices.Protocols.SearchScope.Subtree, null);
DirectoryResponse response = connection.SendRequest(request);
connection.EndSession();
}
}

