分类(.NET技术)
使用WebRequest向页面发送数据
2009/06/08 12:33

Winform程序:
发送数据:
string path = AppDomain.CurrentDomain.BaseDirectory + "1.xml";
byte[] SomeBytes = new byte[1024];
WebRequest req = WebRequest.Create(http://localhost:6601/website/1.aspx);
req.Method = "POST";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);//要发送xml的路径.   
SomeBytes = Encoding.UTF8.GetBytes(xmldoc.OuterXml);
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
MessageBox.Show("发送完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

WebResponse wresp = req.GetResponse();      //接收返回数据
Stream respStream = wresp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
richTextBox1.Text = reader.ReadToEnd();
wresp.Close();

 

Web程序:
接收数据:
Stream s = HttpContext.Current.Request.InputStream;
byte[] buffer = new byte[1024];//无符号 8 位整数数组
int count = 0;
StringBuilder builder = new StringBuilder();//表示可变字符字符串。

while ((count = s.Read(buffer, 0, 1024)) > 0)//每次读取1024个字节
{
builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
}
string msgReturn = builder.ToString();

msgReturn 为接收到的字符串

 

//发送返回数据
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes("http://www.xt80");
HttpResponse hr = HttpContext.Current.Response;
hr.Clear();
hr.OutputStream.Write(bytes, 0, bytes.Length);
hr.OutputStream.Close();

Tags: ASP.NET,常用技术,WebForm
相关日志:
+引用(0) |
发表评论
昵称  密码  游客无需密码
网址  电邮  注册
验证码 请输入左侧图片中的字符
记住我