模拟XmlHtt prequest与.NET Web客户端 [英] Emulate XmlHttpRequest with a .NET WebClient
问题描述
AFAIK与 XmlHtt prequest
我可以下载和上传数据刚好与发送
方法。但 Web客户端
有很多方法。我不希望有一个 Web客户端
的所有功能。我只是想创建一个对象,它模拟了一个 XmlHtt prequest
,但它不具有XSS限制。我也不在乎得到响应为XML,甚至作为一个字符串现在。如果我能得到它的字节是不够好数组。
AFAIK with XmlHttpRequest
I can download and upload data just with the send
method. But WebClient
has plenty of methods. I don't want all the functionality of a WebClient
. I just want to create an object that emulates a XmlHttpRequest
, except that it doesn't have XSS restrictions. I also don't care about getting the response as an XML or even as a string right now. If I can get it as an array of bytes that's good enough.
我以为我可以使用 UploadData code>我的通用方法,而是试图下载的数据与它,即使它返回一个回应时,它会失败。那么,如何可以写一个方法,行为就像
XmlHtt prequest
的发送
的方法?
I thought that I could use the UploadData
as my universal method, but it fails when trying to download data with it even though it returns a response. So how can I write a method that behaves just like XmlHttpRequest
's send
method?
编辑:我发现了一个不完整的类,它是precisely的 XmlHtt prequest
仿真器的此处。太糟糕了,整个code会丢失。
I found an incomplete class that is precisely an XmlHttpRequest
emulator here. Too bad the whole code is lost.
推荐答案
您将需要使用的 HttpWebRequest的。
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("http://thewebsite.com/thepage.html");
using(Stream s = rq.GetRequestStream()) {
// Write your data here
}
HttpWebResponse resp = (HttpWebResponse)rq.GetResponse();
using(Stream s = resp.GetResponseStream()) {
// Read the result here
}
这篇关于模拟XmlHtt prequest与.NET Web客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!