<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<background>
<SourceAddress>200.20.8.10</SourceAddress>
<SourcePort>1234</SourcePort>
<PID>1345</PID>
</background>
</root>
用C#如何实现向远程服务器提交上面的数据?
最好提供示例!
谢谢了!
<root>
<background>
<SourceAddress>200.20.8.10</SourceAddress>
<SourcePort>1234</SourcePort>
<PID>1345</PID>
</background>
</root>
用C#如何实现向远程服务器提交上面的数据?
最好提供示例!
谢谢了!
不过得先给权限
我这个不需要服务器授权,通过HTTP发送,不要用xml文件保存!
有谁知道,给个示例啊!!
winform:string postData = "a=" + textBox1.Text;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://localhost:8801/A.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
System.IO.Stream newStream = req.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
MessageBox.Show(res.StatusDescription.ToString());
newStream.Close();a.aspx
============
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="A" ValidateRequest="false" %>a.aspx.cs
============
protected void Page_Load( object sender, EventArgs e )
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("a.txt"),true,System.Text.Encoding.UTF8);
sw.Write(Request.Form["a"]);
sw.Close();
}
string postData = textBox1.Text;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://localhost:8801/A.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
System.IO.Stream newStream = req.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
MessageBox.Show(res.StatusDescription.ToString());
newStream.Close();
aspx.cs:protected void Page_Load( object sender, EventArgs e )
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Request.InputStream);
doc.Save(Server.MapPath("a.xml"));
}
感谢了!!!
string postData = textBox1.Text;
// 这一段是将输入的xml内容转化为byte数组,
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 这一段使用webrequest发送http消息,模拟浏览器提交的效果
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://localhost:8801/A.aspx");
// 设置http的方法是post,post/get二种方法
req.Method = "POST";
// 模拟form的类型,一般来说用这种类型的form
req.ContentType = "application/x-www-form-urlencoded";
// 数据长度,http要求
req.ContentLength = data.Length;
// 利用二进制流发送数据
System.IO.Stream newStream = req.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
// 得到http的返回结果
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
MessageBox.Show(res.StatusDescription.ToString());
// 关闭流
newStream.Close();