WebService 实现BS环境与BS环境传递参数,根据参数生成txt文档

客户端:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client.aspx.cs" Inherits="客户端.Client" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="script/jquery-1.9.1.js"></script>
<style type="text/css">
#btn {
cursor: pointer;
height:32px;
font-size:15px;
}
</style>
</head>
<body>
<form runat="server" id="from1">
时 间:&nbsp;
<asp:TextBox ID="time" runat="server"></asp:TextBox></br>
地 点:&nbsp;
<asp:TextBox ID="address" runat="server"></asp:TextBox></br>
污染物:&nbsp;<asp:TextBox ID="pollutant" runat="server"></asp:TextBox></br>
投放记录:<asp:TextBox ID="PutonRecord" runat="server"></asp:TextBox></br></br>
<asp:Button ID="btn" runat="server" Text="点击传递参数" OnClick="btn_Click" class="btn02"/>&nbsp;&nbsp;&nbsp;
返回的ID为:<asp:Label ID="Id" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
<script type="text/javascript">
$('#PutonRecord').ready(function () {
$('#PutonRecord').css("width", "400px"),
$('#PutonRecord').css("height", "20px")
});
$('#time').ready(function () {
$('#time').css("width", "400px"),
$('#time').css("height", "20px")
});
$('#address').ready(function () {
$('#address').css("width", "400px"),
$('#address').css("height", "20px")
});
$('#pollutant').ready(function () {
$('#pollutant').css("width", "400px"),
$('#pollutant').css("height", "20px")
});
</script>

客户端后台:

protected void btn_Click(object sender, EventArgs e)
{
ServiceReference1.WebService1SoapClient c = new ServiceReference1.WebService1SoapClient();
string times = time.Text;
string addr = address.Text;
string pollu = pollutant.Text;
string putrecord = PutonRecord.Text;
int id = c.GetData(times, addr, pollu, putrecord);
Id.Text = id.ToString();
}

 

 

服务端  WebService

using Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Services;

namespace 服务端
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
//参数:时间 ,地点, 污染物, 投放记录
int id;
[WebMethod]
public int GetData(string time, string address, string pollutant, string PutonRecord)
{
if (time != null && address != null && pollutant != null && PutonRecord != null)
{
string[] msgs = { time, address, pollutant };
File.Delete(@"C:\Users\jun\Desktop\WaterTools\WaterTools\para.txt");
for (int i = 0; i < msgs.Length; i++)
{
using (StreamWriter write = new StreamWriter(@"C:\Users\jun\Desktop\WaterTools\WaterTools\para.txt", true))
{
write.Write(msgs[i] + "\r\n");
}
}
//污染记录分开写入到para.txt文档中
string[] jilu = PutonRecord.Split(' ');
for (int i = 0; i < jilu.Length; i++)
{
using (StreamWriter wri = new StreamWriter(@"C:\Users\jun\Desktop\WaterTools\WaterTools\para.txt", true))
{
wri.Write(jilu[i] + "\r\n");
}
}
//删除原有res.txt文档
try
{
File.Delete(@"C:\Users\jun\Desktop\WaterTools\WaterTools\res.txt");
}
catch { };
Process.Start(@"C:\Users\jun\Desktop\WaterTools\WaterTools\WaterTools.exe");
Data();
}
return id;
}
public int Data()
{
Thread.Sleep(31000);
DirectoryInfo di = new DirectoryInfo(@"C:\Users\jun\Desktop\WaterTools\WaterTools");
FileSystemInfo[] infos = di.GetFileSystemInfos();
for (int i = 0; i < infos.Length; i++)
{
if (infos[i].Name == "res.txt")
{
//存在
FileStream fs = new FileStream(@"C:\Users\jun\Desktop\WaterTools\WaterTools\res.txt", FileMode.Open);
byte[] buffer = new byte[1024 * 1024 * 5];
//实际读取到的有效字节数
int r = fs.Read(buffer, 0, buffer.Length);
//将字节数组解码成字符串
string str = Encoding.Default.GetString(buffer, 0, r);
fs.Close();
fs.Dispose();
string sql = "insert into Data(Result)values(@Res) SELECT @@IDENTITY";
SqlParameter[] ps = new SqlParameter[] { new SqlParameter("@Res", str) };
object obj = SqlHelper.ExcuteScalar(sql, ps);
string o = obj.ToString();
id = Convert.ToInt32(o);
break;
}
}
return id;
}
}
}

 另外防止调用时间超时 ,只需要在客户端设置sendTimeout="00:05:00"这里是标识5分钟在节点binding中设置就好

服务端需要设置 executionTimeout="300000"在httpRuntime中设置

posted @ 2015-05-25 13:24  anderson_net  阅读(174)  评论(0编辑  收藏  举报