using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; using System.Drawing; using System.Text; using System.Net; using System.Xml;
/// <summary> /// 测试 发送数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { string url = "http://www.ktonsoft.com/GetPosStr.aspx";//发送到的页面的地址 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//读取一个文件 FileStream fs = new FileStream(Server.MapPath("aa.zip"), System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] filecontent = new byte[fs.Length]; fs.Read(filecontent, 0, filecontent.Length); fs.Close(); fs.Dispose(); //将图片转换成base64编码的流 string a = Convert.ToBase64String(filecontent); //读取base64编码流,发送 byte[] requestBytes = System.Text.Encoding.Default.GetBytes(a); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); //接收返回参数,到string backstr HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default); string backstr = sr.ReadToEnd(); sr.Close(); res.Close(); //输出参数 Response.Write(backstr); }
|