}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//这样调用
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Threading;
using System.IO;
using Navi;
using System.Net.Browser;
namespace Silverlight_HttpDemo
{
public partial class MainPage : UserControl
{
private string para = @"
SQL=select * from sys_czyb where rownum < 3
&USER=app_user
&CHARSET=UTF-8
&FUN=select
&COMPRESS=true
&SPLITE_ROW=|
&SPLITE_COL=^
";
private string url = "http://10.111.43.18:8099/pmias/servlet/db_proc_common";
public MainPage()
{
//For Get Http Header
bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
InitializeComponent();
para = para.Replace("\r\n", "");
}
//请求获取文本
private void button1_Click(object sender, RoutedEventArgs e)
{
new HttpUtil().Post(para, url, "GET_TXT", this.http_cb);
}
//请求获取数据后保存
Stream savefile;
private void button2_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
if (sf.ShowDialog() == true)
{
savefile = sf.OpenFile();
new HttpUtil().Post(para,url,"SAVE_FILE",this.http_cb);
}
}
//回调函数
private void http_cb(Boolean success,bool compress,string flag,Stream responseStream)
{
String fmt = @"成功:{0}
压缩:{1}
内容:{2}";
string content = "未定义";
switch (flag)
{
case "SAVE_FILE":
{
if (success == false)
{
//失败,不写入文件,取出错误信息
if (compress)
content = ZipUtil.ZipStream2String(responseStream);
else
content = ZipUtil.Stream2String(responseStream);
savefile.Close();
}
else
{
//成功,将返回的流写入文件
if (compress)
ZipUtil.ZipStream2File(savefile, responseStream);
else
ZipUtil.Stream2File(savefile, responseStream);
savefile.Close();
content = "成功";
}
content = String.Format(fmt, success, compress, content);
}
break;
case "GET_TXT":
{
if (compress)
content = ZipUtil.ZipStream2String(responseStream);
else
content = ZipUtil.Stream2String(responseStream);
content = String.Format(fmt, success, compress, content);
}
break;
default:
MessageBox.Show("错误的flag:"+flag);
break;
}
MessageBox.Show(content, "提示", MessageBoxButton.OK);
}
}
}