using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Threading;
using System.Text;
namespace Navi
{
/*
* 钟磊 20111127
*
* 封装silverlight 中的Http Post调用
*
* 使用时需要在应用的第一个构造函数中添加语句,否则无法取到Http响应头
*
* bool registerResult = WebRequest.RegisterPrefix("http://",WebRequestCreator.ClientHttp);
*
* 形如:
*
* public MainPage()
{
//For Get Http Header
bool registerResult = WebRequest.RegisterPrefix("http://",WebRequestCreator.ClientHttp);
InitializeComponent();
* }
*/
public class HttpUtil
{
/*
* *
* HTTP_CALLBACK 用户调用传入的回调函数
* success 调用是否成功
* compress 返回的stream是否是zip过的
* flag 调用时传入的标记,用以在调用端区分是哪一个的回调
* responseStream,Http响应传回的流
*/
public delegate void HTTP_CALLBACK(bool success,bool compress,string flag,Stream responseStream);
private byte[] _postData = null;
private SynchronizationContext _syncContext = null;
private HTTP_CALLBACK _cb = null;
private string _flag;//当前调用的动作
public HttpUtil(){}
public void Post(string strPara,string strUrl,string strFlag,HTTP_CALLBACK cb)
{
//保存当前调用线程上下文(用户线程)
_syncContext = SynchronizationContext.Current;
_cb = cb;
_postData = Encoding.UTF8.GetBytes(strPara);
_flag = strFlag;
Uri endpoint = new Uri(strUrl,UriKind.Absolute);
HttpWebRequest request = WebRequest.Create(endpoint) as HttpWebRequest;
request.Method = "POST";
IAsyncResult asyncResult = request.BeginGetRequestStream(
new AsyncCallback(callback_resuest),request);
}
private void callback_resuest(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.EndGetRequestStream(result);
requestStream.Write(_postData,_postData.Length);
requestStream.Close();
request.BeginGetResponse(new AsyncCallback(callback_response),request);
}
private void callback_response(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
WebResponse response = request.EndGetResponse(asyncResult);
//回调界面,返回用户线程
_syncContext.Post(callback_ui,response);
}
private void callback_ui(object state)
{
HttpWebResponse response = state as HttpWebResponse;
//与服务端约定返回数据是否压缩(ZIP)标志
bool compress = (response.Headers["COMPRESS"].toupper() == "TRUE");
//与服务端约定的成功标志
bool success = (response.Headers["RESULT_CODE"].toupper() == "OK");
//回调
_cb(success,compress,_flag,response.GetResponseStream());
response.GetResponseStream().Close();
response.Close();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//这样调用
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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,"SAVE_FILE",this.http_cb); } } //回调函数 private void http_cb(Boolean success,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,content); } break; case "GET_TXT": { if (compress) content = ZipUtil.ZipStream2String(responseStream); else content = ZipUtil.Stream2String(responseStream); content = String.Format(fmt,content); } break; default: MessageBox.Show("错误的flag:"+flag); break; } MessageBox.Show(content,"提示",MessageBoxButton.OK); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。