- 이해한 문의내용
C# 클라이언트에서 php server로 post할 경우 php에서 넘어온 post값이 제대로 넘어 오지 않는다?
해당사항을 기준으로 테스트를 해보았습니다.
- php server - laragon php 5.4.9
[c# 클라이언트]
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Text.Json;
using System.Net.Http;
public string WebRequestJson(string url, byte[] byteArray)
{
Console.WriteLine(String.Format("request:\r\n{0}\r\n----------------------------", Encoding.Default.GetString(byteArray)));
string ret = string.Empty;
//byte[] byteArray = Encoding.UTF8.GetBytes(postData);
var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.ContentType = "application/x-www-form-urlencoded;";
webRequest.ContentLength = byteArray.Length;
webRequest.Method = "POST";
Stream dataStream = null;
try
{
dataStream = webRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
catch (WebException we)
{
}
catch (Exception e)
{
}
finally
{
if (dataStream != null)
{
dataStream.Close();
}
}
}
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
ret = reader.ReadToEnd();
Console.WriteLine(String.Format("response:\r\n{0}\r\n----------------------------", ret));
return ret;
}
var parameters = new Dictionary<string, string>();
parameters.Add("aaa", "1234");
parameters.Add("bbb", "5678");
var encodedContent = new FormUrlEncodedContent(parameters);
var byteContent = await encodedContent.ReadAsByteArrayAsync();
WebRequestJson("http://localhost/ReceivePostData.php", byteContent);
[php server(http://localhost/ReceivePostData.php)]
<?php echo $_POST['aaa']; ?>
<?php echo $_POST['bbb']; ?>
<?php
print_r($_GET);
print_r($_POST);
?>