简体   繁体   中英

Emulate XmlHttpRequest with a .NET WebClient

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.

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?

Edit: I found an incomplete class that is precisely an XmlHttpRequest emulator here . 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 :)

You'll need to use an 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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