【发布时间】:2014-09-04 20:54:12
【问题描述】:
谁能解释我如何使用不同类型的数据向 Web 上的 URL 发出 POST 请求,在我的情况下,我有一个图像和两个字符串类型的值要发送到 PHP 中的服务器。
在这里我已经做了什么
var stream = await file.OpenStreamForReadAsync();
var streamcontent = new StreamContent(stream);
streamcontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "photo",
FileName = file.Name
};
streamcontent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
MultipartFormDataContent multipart = new MultipartFormDataContent();
multipart.Add(streamcontent);
try
{
descContent = mytextbox.Text;
var stringcontent = new StringContent(descContent);
stringcontent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("description", descContent));
multipart.Add(stringcontent);
HttpResponseMessage res = await client.PostAsync(new Uri("http://localhost/web/test/index.php"), multipart);
res.EnsureSuccessStatusCode();
mytextbox.Text = await res.Content.ReadAsStringAsync();
}
catch (HttpRequestException ex)
{
mytextbox.Text = ex.Message;
}
此代码将发送图像文件,但不发送描述(字符串),我在互联网上搜索过,但找不到合适的答案。
这是PHP代码
if (isset($_FILES['photo']))
{
echo $_FILES["photo"]["name"] . "<br>";
}
else
{
echo "Image: Error<br>";
}
if (isset($_POST['description']))
{
echo $_POST['description'];
}
else
{
echo "Text: Error";
}
我们将不胜感激。
谢谢你
【问题讨论】:
标签: c# php webclient windows-phone-8.1 multipartform-data