【问题标题】:HttpWebRequest/HttpResponse: How to send data in the response?HttpWebRequest/HttpResponse:如何在响应中发送数据?
【发布时间】:2011-01-15 20:41:02
【问题描述】:

我有一个客户端和一个服务器。

在客户端我有:

HttpWebRequest request = 
    (HttpWebRequest)WebRequest.Create("http://localhost/fa/Default.aspx");
request.Method = "POST";                

byte[] data = Encoding.ASCII.GetBytes(GetSAMLRequestB64());

request.ContentType = "text/xml";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

在服务器端我有:

public void ProcessRequest(HttpContext httpContext) 
{
    HttpResponse response = httpContext.Response;             
    response.Clear();
    response.BufferOutput = true;
    response.StatusCode = 200; // HttpStatusCode.OK;
    response.Write("Hello");
    response.ContentType = "text/xml";
    response.End();
}

客户端收到带有正确StatusCode 的响应。虽然,如果我在客户端上执行(int)response.ContentLength;,我会得到 0。在收到响应(客户端)后,我无法读取字符串“Hello”。

【问题讨论】:

标签: c# .net httpwebrequest httpresponse


【解决方案1】:

也许在实际写入或刷新流之前设置内容类型会有所帮助。

【讨论】:

  • 我这样做了,但是有一个复制/过去的问题......所以,仍然不适用于内容类型。
【解决方案2】:

您没有在服务器上设置 ContentLength。也许这会有所帮助?

【讨论】:

  • 如何在服务器上设置 contentLength?
  • response.ContentLength = n; 不起作用?如果不是,那么我误认为ContentLength 是您的问题。
猜你喜欢
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2020-09-12
  • 2019-10-09
  • 1970-01-01
  • 2023-02-21
相关资源
最近更新 更多