使用するEncodingはUTF-8を仮定しています。
public static void PostData(string host, int port, string data)
{
try
{
Uri requestUri = new Uri(string.Format("http://{0}:{1}/", host, port));
WebRequest request = WebRequest.Create(requestUri);
request.Proxy = new WebProxy();
request.Method = "POST";
byte[] postDataBytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = postDataBytes.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postDataBytes, 0, postDataBytes.Length);
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string strResp = reader.ReadToEnd();
// Do something...
}
}
}
catch (Exception ex)
{
log.ErrorFormat("Error in {0} method\n{1}", MethodBase.GetCurrentMethod().Name, ex.ToString());
}
}
コメント