开发者

HttpWebRequest/HttpResponse: How to send data in the response?

开发者 https://www.devze.com 2022-12-20 02:21 出处:网络
I have a client and a server. On the client side I have: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(\"http://localhost/fa/Default.aspx\");

I have a client and a server.

On the client side I have:

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();

On the server side I have:

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();
}

The client receives the response with the correct StatusCode. Although, if I do (int)response.ContentLength; on the client I get 0. I can't read the string "Hello" after I receive the response (client side).


Perhaps setting the content type before the actual write or flushing the stream would help.


You didn't set ContentLength on the server. Maybe that would help?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号