如何使用Stream发送字符串

时间:2014-02-04 12:05:36

标签: c# asp.net string asp.net-mvc-3 stream

我正在查看用于传输数据的Stream示例,我想传递一个额外的字符串'infoAsString'

string infoAsString = "blablabla";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(message);
            request.Method = "POST";
            request.ContentType = "text/xml;charset=utf-8";
            request.ContentLength = requestBytes.Length;

            using (Stream requestStream = request.GetRequestStream())
            {      
                //pass infoAsString?
                requestStream.Write(requestBytes, 0, requestBytes.Length);

                requestStream.Close();
            }

//然后我可以抓住它..

public object Upload(string infoAsString)
    {

请告知....感谢任何回复

1 个答案:

答案 0 :(得分:1)

您可以使用request.Headers集合:

string infoAsString = "blablabla";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(message);
request.Method = "POST";
request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = requestBytes.Length;
request.Headers.Add(string.format("infoAsString: {0}", infoAsString))

using (Stream requestStream = request.GetRequestStream())
{      
    requestStream.Write(requestBytes, 0, requestBytes.Length);

    requestStream.Close();
}