Emulate XmlHttpRequest with a .NET WebClient

孤街浪徒 提交于 2019-12-09 04:24:34

问题


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.


回答1:


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
}



回答2:


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 :)



来源:https://stackoverflow.com/questions/6079282/emulate-xmlhttprequest-with-a-net-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!