使用XMLHTTP下载远程文件,提交内容
2010年9月22日
没有评论
xmlhttp用在异步或同步通信都比较方便,.net的XMLHTTP类库更加为我们提供了很多的方便
//GET protected void Button1_Click(object sender, EventArgs e) { MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); string Url = "http://kobewang.cn/readme.txt"; _xmlhttp.open("GET", Url, false, null, null); _xmlhttp.send(""); if (_xmlhttp.readyState == 4) { string response = _xmlhttp.responseText; if (response.Length > 0) { string content = Encoding.GetEncoding("GB2312").GetString((byte[])_xmlhttp.responseBody); Response.Write("<br/>responseBody:" + content); Response.End(); } else { Response.Write("No Data!"); } } } //POST protected void Button2_Click(object sender, EventArgs e) { MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); string Url = "http://kobewang.cn/test.asp"; //要提交的内容 string post = "query=11&query2=22"; _xmlhttp.open("POST",Url,false,null,null); _xmlhttp.setRequestHeader("Content-Length",post.Length.ToString()); _xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); _xmlhttp.send(post); //返回的内容 string returnStr = _xmlhttp.responseText.Trim(); _xmlhttp.abort(); _xmlhttp = null; Response.Write(returnStr); }