douxiegan6468 2018-07-19 21:05
浏览 62

PHP控制用于C#应用程序的MySQL数据库

I am trying to make a C# application that connects to a MySQL database through PHP. My PHP code is the following:

<?php
require_once(dirname(__FILE__).'/dbconnect.php');

if (isset($_GET['name']) && isset($_GET['number']))
{
    //Get the POST variables
    $mName = $_GET['name'];
    $mNumber = $_GET['number'];

    //Insert new contact into database
    $sql = "INSERT INTO `products`(`name`) VALUES (\"" . $mName . "\")";

    echo $sql;

    //Execute query
    $stmt = mysqli_query($con, $sql); 
    if (!$stmt)
    {   
        echo 'Query Failed';     
    }
    else
    {
        echo "Query Successful";
    }
}

mysqli_close($con);
?>

In my C# application I use a WebClient and the method DownloadString to make a request.

Uri uri = new Uri("http://manoest.dk/UpdateValue.php?name=John&number=23");

WebClient client = new WebClient();
client.Headers.Add("Content-Type", "text/html; charset=iso-8859-1");
//client.Headers.Add("Accept", "text/plain");
client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");


var streng = client.DownloadString(uri);

I have tried many different things, but everytime I will get an 406 error (Not acceptable). I guess that the headers are the problem, but I have tried and can't see the problem.

  • 写回答

1条回答 默认 最新

  • doy2255 2018-07-19 21:29
    关注

    You need to add a header call in the PHP file to indicate what content-type you're producing. For example, for simple text:

    header('Content-Type: text/plain');
    

    Add this call before producing any output to avoid problems. See http://php.net/manual/en/function.header.php

    Then, add the same content-type in your C# client as your Accept header.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效