主要功能:定时启动,调用webservice,将返回的xmlstring上传给服务器。

定时启动使用的是timer组件,工具箱里面的timer控件没有Elapsed事件,所以要在程序里面写private System.Timers.Timer timer1;程序主要的功能都是在private void timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)函数中完成的;调用webservice是直接添加的web References在本地生成代理类,就可以直接调用了;上传xmlstring是利用HttpWebRequest HttpWebResponse这两个类。

 


程序有两个入口参数:定时启动的时间和上传
xmlstring所在服务器的IP,这个是用c++写的控制面板程序,服务启动的时候会到注册表中读取这两个值。。

 

注意:服务的Account是设为user(我设的是隶属于Administrator的帐户),其他三种类型的帐号都不适用。

下面是程序的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Xml;
using System.Web;
using Microsoft.Win32;
using System.Net;

namespace ServiceText
{
    
public partial class Service1 : ServiceBase
    
{
        
private System.Timers.Timer timer1;

        
static string strTime = "";

        
static string strIp = "";

        
public CookieContainer cookie = new CookieContainer();

        
public Service1()
        
{

            InitializeComponent();

        }


        
protected override void OnStart(string[] args)
        
{
            
// TODO: Add code here to perform any tear-down necessary to start your service.

            
this.timer1.Enabled = true;

            WriteLog(
"服务开始启动。。");

            readKey();

          
        }


        
protected override void OnStop()
        
{
            
// TODO: Add code here to perform any tear-down necessary to stop your service.
            this.timer1.Enabled = false;

            WriteLog(
"服务已经停止。。");
        }


        
private void readKey()//读注册表
        {

            RegistryKey serviceKey 
= Registry.LocalMachine.OpenSubKey("System");

            RegistryKey myserviceKey 
= serviceKey.OpenSubKey("CurrentControlSet");

            RegistryKey ohmyserviceKey 
= myserviceKey.OpenSubKey("Services");

            RegistryKey ohmygodserviceKey 
= ohmyserviceKey.OpenSubKey("ServiceText");
 
            strTime 
= (string)ohmygodserviceKey.GetValue("time");

            strIp 
= (string)ohmygodserviceKey.GetValue("ip");
             
        
 
        }



        
private void timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
        
{

            
if (DateTime.Now.ToString("HH:mm"== strTime)// the time
            {
                WriteLog(
"定时程序开始运行。。");

                GetandPutXml();
             
             
            }

           
        }


        
public static void WriteLog(string strLog)
        
{

            
string strPath;

      
//    strPath = System.Environment.SystemDirectory;

            strPath 
= @"c:\log.txt";

            FileStream fs 
= new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter m_streamWriter 
= new StreamWriter(fs);

            m_streamWriter.BaseStream.Seek(
0, SeekOrigin.End);

            m_streamWriter.WriteLine(strLog);

            m_streamWriter.Flush();

            m_streamWriter.Close();

            fs.Close();

        }




        
public void GetandPutXml()
        
{
            RichMall.RichMall ws 
= new ServiceText.RichMall.RichMall();

            
string xmlstring1 = ws.GetData();

            
string page = login();

            
if (page.IndexOf("<Value>1</Value>"> 0)//服务器返回的xml
            {
                LoadupFile(xmlstring1);
            }

            logout();
        }


        
public string login()
        
{
            
string url = "http://" + strIp + "/xia/server/xia.php?COMMAND=Login&Account=puretime&Password=54321";//呵呵,这IP无效

            HttpWebRequest myRequest 
= (HttpWebRequest)WebRequest.Create(url);

            myRequest.AllowAutoRedirect 
= true;

            myRequest.CookieContainer 
= cookie;

            HttpWebResponse myresponse 
= (HttpWebResponse)myRequest.GetResponse();

            myresponse.Cookies 
= cookie.GetCookies(myRequest.RequestUri);

            Stream mystream 
= null;

            mystream 
= myresponse.GetResponseStream();

            StreamReader myreader 
= new StreamReader(mystream, System.Text.Encoding.Default, true);

            
string pagefile = myreader.ReadToEnd();

            WriteLog(pagefile);

            
return pagefile;

        }



        
public void LoadupFile(string xmlstring)
        
{

                
string postUrl = "http://" + strIp + "/xia/Server/xia.php?CMD=Add";  //上传xml

                HttpWebRequest Request 
= (HttpWebRequest)WebRequest.Create(postUrl);

                Request.CookieContainer 
= cookie;

                Request.Method 
= "POST";

                Request.ContentType 
= "application/x-www-form-urlencoded";

                Request.AllowAutoRedirect 
= true;

                
//    string strXML = "XMLDATA=<book><author>Irina</author><title>Piano Fort A</title><price>4.95</price></book>";

                
string strXML = "XMLDATA=" + xmlstring;

                
byte[] data = Encoding.UTF8.GetBytes(strXML);

                Stream newStream 
= Request.GetRequestStream();

                newStream.Write(data, 
0, data.Length);

                newStream.Close();

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

                Stream stream 
= null;

                stream 
= response.GetResponseStream();

                StreamReader reader 
= new StreamReader(stream, System.Text.Encoding.Default, true);

                
string file = reader.ReadToEnd();

                WriteLog(file);

        }


        
public void logout()
        
{
            
string outUrl = "http://" + strIp + "/xia/Server/xia.php?CMD=Logout "//登出

            HttpWebRequest outRequest 
= (HttpWebRequest)WebRequest.Create(outUrl);

            outRequest.AllowAutoRedirect 
= true;

            outRequest.CookieContainer 
= cookie;

            HttpWebResponse outresponse 
= (HttpWebResponse)outRequest.GetResponse();

            Stream outstream 
= null;

            outstream 
= outresponse.GetResponseStream();

            StreamReader outreader 
= new StreamReader(outstream, System.Text.Encoding.Default, true);

            
string outfile = outreader.ReadToEnd();

            WriteLog(outfile);
        }


         


    }

}

namespace ServiceText
{
    partial 
class ProjectInstaller
    
{
        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
/// <summary> 
        
/// Clean up any resources being used.
        
/// </summary>
        
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


    
//    #region Component Designer generated code

        
/// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
            
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();

            
            
// 
            
// serviceProcessInstaller1
            
// 
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;

            
this.serviceProcessInstaller1.Username = null;

            
this.serviceProcessInstaller1.Password = null;
           
            
// 
            
// serviceInstaller1
            
// 
            this.serviceInstaller1.Description = "tigger the program of webservice on time,then get xml string,and send them to another service.";
         
            
this.serviceInstaller1.ServiceName = "ServerText";

            
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

//         初始化注册表里的值
            Microsoft.Win32.RegistryKey serviceKey 
= Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System"true);

            Microsoft.Win32.RegistryKey myserviceKey 
= serviceKey.CreateSubKey("CurrentControlSet");

            Microsoft.Win32.RegistryKey ohmyserviceKey 
= myserviceKey.CreateSubKey("Services");

            Microsoft.Win32.RegistryKey ohmygodserviceKey 
= ohmyserviceKey.CreateSubKey("ServerText ");

            ohmygodserviceKey.SetValue(
"time""20:00");

            ohmygodserviceKey.SetValue(
"ip""192.168.1.1");  
            
// 
            
// ProjectInstaller 
            
// 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            
this.serviceProcessInstaller1,
            
this.serviceInstaller1}
);

        }


      
//  #endregion

        
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
        
public System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
    }

}

namespace ServiceText
{
    partial 
class Service1
    
{
        
/// <summary> 
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>
        
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


        
Component Designer generated code

    }

}
posted on 2007-06-01 13:34  下风  阅读(653)  评论(0编辑  收藏  举报