Sie sind auf Seite 1von 4

Web Service

Retrieve / Publish Methods


Web IP Address Information
[WebMethod(Description="Get Server IP Address")]
public string GetServerIPAddress()
{
// used to build entire input
StringBuilder sb = new StringBuilder();

// used on each read operation


byte[] buf = new byte[8192];

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://www.network-tools.com");

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// we will read data via the response stream


Stream resStream = response.GetResponseStream();

string tempString = null;


int count = 0;

do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);

// make sure we read some data


if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);

// continue building the string


sb.Append(tempString);
}
}
while (count > 0); // any more data to read

String __SearchString =
"<input id=\"field\" name=\"host\" type=\"text\" value =\"(.*)\" size=\"85\" />";

System.Text.RegularExpressions.Regex _regex =
new System.Text.RegularExpressions.Regex(__SearchString);

return _regex.Match(sb.ToString()).Result("$1").ToString();
}
[WebMethod(Description = "Publish Server IP Address")]
public string publishServerIP()
{
string IP = GetServerIPAddress();
string company = "{your company ftp information goes here}";
string fileName = "{your filename goes here}";
string userName = "{your userName goes here} ";
string password = "{your passWord goes here}";

string FTPAddress = String.Format(


"ftp://{0}/{1}",
company,
fileName);

FtpWebRequest putFTP;
putFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress));
putFTP.Credentials = new NetworkCredential( userName, password);
putFTP.KeepAlive = false;
putFTP.Method = WebRequestMethods.Ftp.UploadFile;
putFTP.UseBinary = true;
putFTP.UsePassive = true;

ASCIIEncoding encoding = new ASCIIEncoding();

putFTP.ContentLength = encoding.GetBytes(IP).Length;

byte[] buff = new byte[2048];


buff = encoding.GetBytes(IP);

TextWriter tw = new StreamWriter(


File.Open(Server.MapPath("App_Data/" + fileName.Replace("txt","log")),
FileMode.Append, FileAccess.Write)
);

try
{
Stream strm = putFTP.GetRequestStream();
strm.Write(buff, 0, buff.Length);
strm.Close();
string logMessage = String.Format("{0} {1} {2}",
DateTime.Now.ToString(),
FTPAddress.ToString(),
"Failure");
tw.WriteLine(logMessage.ToString());
tw.Flush();
tw.Close();

return "Success";
}
catch (Exception ex)
{
string logMessage = String.Format("{0} {1} {2}",
DateTime.Now.ToString(),
FTPAddress.ToString(),
"Failure");
tw.WriteLine(logMessage.ToString());
tw.WriteLine(ex.ToString());
tw.Flush();
tw.Close();
return "Failure";
}
}

Das könnte Ihnen auch gefallen