1 这段代码是在网上搜到的,拿来共享,项目正好要用到。其中的data你只需要传递一个xml字符串就可以
protected string PostXmlToUrl(string url,string data)
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "POST";
Stream stream = hwr.GetRequestStream();
StreamWriter sw = new StreamWriter(stream,System.Text.Encoding.UTF8);
sw.Write(data);
sw.Close();
stream = hwr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(stream,System.Text.Encoding.UTF8);
string ret = sr.ReadToEnd();
sr.Close();
return ret;
}
2 利用xmlhttp对象,来发送请求.
StreamReader sr=new StreamReader(Server.MapPath(XMLPathFull));
MSXML2.XMLHTTPClass xmlHttp = new MSXML2.XMLHTTPClass();
xmlHttp.open("post",strURL,false,"","");
xmlHttp.setRequestHeader ("Content-type","text/xml;charset=UTF-8");
xmlHttp.send(sr.ReadToEnd());
//得到response
string backxmlstring=xmlHttp.responseText.ToString();
解读这个响应的话只需要2行代码,
byte[] buf = Request.BinaryRead(Request.ContentLength);
string str = System.Text.Encoding.UTF8.GetString(buf);
这样就能得到。
其实以上代码就可以建立一个简单的服务器间的交互模式,模拟的b/s架构,跟使用webservice与服务器间交互差不多,soap和http其实比较类似的。
protected string PostXmlToUrl(string url,string data)
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "POST";
Stream stream = hwr.GetRequestStream();
StreamWriter sw = new StreamWriter(stream,System.Text.Encoding.UTF8);
sw.Write(data);
sw.Close();
stream = hwr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(stream,System.Text.Encoding.UTF8);
string ret = sr.ReadToEnd();
sr.Close();
return ret;
}
2 利用xmlhttp对象,来发送请求.
StreamReader sr=new StreamReader(Server.MapPath(XMLPathFull));
MSXML2.XMLHTTPClass xmlHttp = new MSXML2.XMLHTTPClass();
xmlHttp.open("post",strURL,false,"","");
xmlHttp.setRequestHeader ("Content-type","text/xml;charset=UTF-8");
xmlHttp.send(sr.ReadToEnd());
//得到response
string backxmlstring=xmlHttp.responseText.ToString();
解读这个响应的话只需要2行代码,
byte[] buf = Request.BinaryRead(Request.ContentLength);
string str = System.Text.Encoding.UTF8.GetString(buf);
这样就能得到。
其实以上代码就可以建立一个简单的服务器间的交互模式,模拟的b/s架构,跟使用webservice与服务器间交互差不多,soap和http其实比较类似的。