HttpWebRequestとHttpWebResponseの例
1999 ワード
HttpWebRequestの詳細な使用方法については、MSDNと私のもう1編という例を参照してください.この例は、クライアントが要求を開始しただけで、サービス側は対応する例を簡単に処理します.
クライアント要求
サービス側対応
クライアント要求
string reqestMsg = "Tom";
string responseMsg = string.Empty;
byte[] buffer = Encoding.UTF8.GetBytes(reqestMsg);
try
{
// http://www.cnblogs.com
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:6603/WebServer.aspx");
request.Method = "POST";
request.ContentLength = buffer.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(buffer, 0, buffer.Length);
}
HttpWebResponse resonse = (HttpWebResponse)request.GetResponse();
Stream responseStream = resonse.GetResponseStream();
using (StreamReader sr = new StreamReader(responseStream))
{
responseMsg = sr.ReadToEnd();
}
resonse.Close();
}
catch (Exception ex)
{
responseMsg = ex.Message;
}
Response.Write(responseMsg);
サービス側対応
string responseMsg = "NO MSG";
if (Request.InputStream != null)
{
byte[] buffer = new byte[Request.InputStream.Length];
Request.InputStream.Read(buffer, 0, buffer.Length);
string msg = Encoding.UTF8.GetString(buffer);
msg = msg.Replace("+", "%2B").Replace(" ", "%20");
responseMsg = SayHello(msg);
}
Response.Write(responseMsg);