8:關閉Request流.
dataStream.Close();
這是必須的,知道Close,服務器不會響應輸出,還可以選擇調用System.IO.Stream.Close()關閉流.
9:利用GetResponse()獲取Response對象.
WebResponse Objresponse=ObjWebRequest.GetResponse();
9*:你可以獲取返回給客戶端的HTTP輸出狀態的字符串,默認是公共狀態消息"OK"
//Console.WriteLine(((HttpWebResponse)Objresponse).StatusDescription);
if(((HttpWebResponse)Objresponse).StatusDescription!="OK"){
//..結束這一切
}
10:獲取響應流的句柄:
Stream data=Objresponse.GetResponseStream
11:保存數據或者直接讀出,例如直接輸出到浏覽器.
dataStream=ObjResponse.GetResponseStream();
StreamReader r=new StreamReader(dataStream);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
或者使用StreamWriter對象保存:
Stream outStream=System.IO.File.Create("Temp.data");
byte []buffer=new byte[1024];
int l;
do{
l=dataStream.Read(buffer,0,buffer.Length);
if(l>0)outStream.Write(buffer,0,l);
}while(l>0);
outStream.Close();
此方式可以保存二進制數據.
12:關閉流.
Objresponse.Close();
例如發送Soap文件時候,可以創建XMLDocument對象,直接使用Save()即可:
XmlDocument doc=new XMLDocument();
doc.Load(System.Web.HttpContext.Current.Server.MapPath(@"~")+@""+XMLfile);
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(@"http://s:81/5do8/webs.asmx");
req.ContentType="text/XML;charset="utf-8"";
req.Accept="text/XML";
req.Method="POST";
Stream stm=req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp=req.GetResponse();
stm=resp.GetResponseStream();
StreamReader r=newStreamReader(stm);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));