dsb12300 2014-10-24 21:01
浏览 29
已采纳

将数据发布到php

I ran into a problem lately:

C# code:

string URI = "http://cannonrush.tk/UpdateLogFile.php";
string myParameters = "text=test123";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
    Console.WriteLine(HtmlResult);   
}

PHP file:

<?php
if (isset($_GET['text'])) {
    $file = "LogFile.txt";
    $handle = fopen($file, 'a') or die('ERROR: Cannot write to file: ' . $file);
    date_default_timezone_set('Europe/Amsterdam');
    $data = '~ ' . date('l jS \of F Y h:i:s A') . '>> ' . $_GET['text'] . "

";
    fwrite($handle, $data);
    fclose($handle);
    echo "SUCCESS";
} else {
    echo "ERROR: Access forbidden without text";
}
?>

Now, whenever I run the above C# code, I get this output printed:

ERROR: Access forbidden without text

I have tried numerous versions to post data from C# to php, but nothing seems to work.

Any ideas?

  • 写回答

2条回答 默认 最新

  • doufu9836 2014-10-24 21:18
    关注

    Change $_GET["text"] to $_POST["text"] in the php file, or use DownloadString() instead of UploadString() in your C# code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效