简体   繁体   English

使用.NET WebClient模拟XmlHttpRequest

[英]Emulate XmlHttpRequest with a .NET WebClient

AFAIK with XmlHttpRequest I can download and upload data just with the send method. 带有XmlHttpRequest AFAIK我可以使用send方法下载和上传数据。 But WebClient has plenty of methods. WebClient有很多方法。 I don't want all the functionality of a WebClient . 我不想要WebClient所有功能。 I just want to create an object that emulates a XmlHttpRequest , except that it doesn't have XSS restrictions. 我只是想创建一个模拟XmlHttpRequest的对象,除了它没有XSS限制。 I also don't care about getting the response as an XML or even as a string right now. 我也不关心将响应作为XML或甚至现在作为字符串。 If I can get it as an array of bytes that's good enough. 如果我可以把它作为一个足够好的字节数组。

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. 我认为我可以使用UploadData作为我的通用方法,但是当尝试使用它下载数据时它会失败,即使它返回响应。 So how can I write a method that behaves just like XmlHttpRequest 's send method? 那么如何编写一个行为与XmlHttpRequestsend方法一样的方法呢?

Edit: I found an incomplete class that is precisely an XmlHttpRequest emulator here . 编辑:我在这里找到了一个不完整的类,它正是一个XmlHttpRequest模拟器。 Too bad the whole code is lost. 太糟糕了,整个代码都丢失了。

You can try this static function to do the same 您可以尝试使用此静态函数来执行相同操作

public static string XmlHttpRequest(string urlString, string xmlContent)
{
    string response = null;
    HttpWebRequest httpWebRequest = null;//Declare an HTTP-specific implementation of the WebRequest class.
    HttpWebResponse httpWebResponse = null;//Declare an HTTP-specific implementation of the WebResponse class

    //Creates an HttpWebRequest for the specified URL.
    httpWebRequest = (HttpWebRequest)WebRequest.Create(urlString);

    try
    {
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(xmlContent);
        //Set HttpWebRequest properties
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentLength = bytes.Length;
        httpWebRequest.ContentType = "text/xml; encoding='utf-8'";

        using (Stream requestStream = httpWebRequest.GetRequestStream())
        {
            //Writes a sequence of bytes to the current stream 
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();//Close stream
        }

        //Sends the HttpWebRequest, and waits for a response.
        httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        if (httpWebResponse.StatusCode == HttpStatusCode.OK)
        {
            //Get response stream into StreamReader
            using (Stream responseStream = httpWebResponse.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream))
                    response = reader.ReadToEnd();
            }
        }
        httpWebResponse.Close();//Close HttpWebResponse
    }
    catch (WebException we)
    {   //TODO: Add custom exception handling
        throw new Exception(we.Message);
    }
    catch (Exception ex) { throw new Exception(ex.Message); }
    finally
    {
        httpWebResponse.Close();
        //Release objects
        httpWebResponse = null;
        httpWebRequest = null;
    }
    return response;
}

hnd :) h :)

You'll need to use an HttpWebRequest . 您需要使用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM