【问题标题】:Emulate XmlHttpRequest with a .NET WebClient使用 .NET WebClient 模拟 XmlHttpRequest
【发布时间】:2011-08-30 01:45:46
【问题描述】:

XmlHttpRequest AFAIK 我可以使用send 方法下载和上传数据。但是WebClient 有很多方法。我不想要WebClient 的所有功能。我只想创建一个模拟XmlHttpRequest 的对象,但它没有 XSS 限制。我现在也不关心以 XML 甚至字符串的形式获取响应。如果我能得到一个字节数组就足够了。

我认为我可以使用UploadData 作为我的通用方法,但是在尝试使用它下载数据时它会失败,即使它返回了响应。那么如何编写一个行为与XmlHttpRequestsend 方法一样的方法呢?

编辑:我发现了一个不完整的类,它恰好是一个XmlHttpRequest 模拟器here。太糟糕了,整个代码都丢失了。

【问题讨论】:

    标签: c# .net winforms xmlhttprequest webclient


    【解决方案1】:

    你可以试试这个静态函数来做同样的事情

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

    【讨论】:

      【解决方案2】:

      您需要使用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
      }
      

      【讨论】:

      • @jsoldi:HttpWebRequest 并没有过时 - 构造函数是。你不再打电话给new HttpWebRequest("url"),你打电话给(HttpWebRequest)WebRequest.Create("url")
      • 注意:您不能真正用HttpWebRequest 代替XmlHttpRequest,因为HttpWebRequest 让您无法读取某些http 状态编号的响应文本 -而是选择抛出异常。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多