将参数发送到URL

时间:2014-03-02 15:27:56

标签: c# php windows-phone-8 dotnet-httpclient

我正在使用HttpClient将一些文本框的内容发送到我的网站。我的PHP脚本将数据插入数据库中。 但是在单击btnSubmit按钮后,它会运行代码但是没有任何内容添加到我的数据库中,也没有抛出任何异常。我的代码出了什么问题?

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Net.Http;

namespace PhoneApp2
{
    public partial class Submit2 : PhoneApplicationPage
    {

        public Submit2()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string parameter = string.Empty;
            if (NavigationContext.QueryString.TryGetValue("barcode", out parameter))
            {
                Barcode.Text = parameter;
            }
        }

        public static int typeConnection()
        {
            switch (Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType)
            {
                default:
                    return 0;
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandCdma:
                    return 1;
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandGsm:
                    return 1;
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.None:
                    return 2;
            }
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (typeConnection() < 2)
                {
                    string URI = "http://cocktailpws.net23.net/requests/add_contribution.php";
                    string myParameters = "barcode=" + Barcode.Text + "&booze=" + Name.Text + "&email=" + Email.Text;
                    sendData(URI, myParameters);
                }
                else
                {
                    MessageBox.Show("No database connection could be established. Please check your internet connection and try again.");
                }
            }
            catch (Exception myExc)
            {
                Console.WriteLine(myExc.Message);
            }
        }

        public async void sendData(string URI, string myParameters)
        {
            using (HttpClient hc = new HttpClient())
            {
                var response = await hc.PostAsync(URI, new StringContent(myParameters));
            }
        }
    }
}

PHP:

if(isset($_POST['barcode']) && isset($_POST['booze']) && isset($_POST['email'])){
    include_once "../inc/inc_db.php";
    $booze = sqlesc($_POST['booze']);
    $barcode = sqlesc($_POST['barcode']);
    $email = sqlesc($_POST['email']);
    date_default_timezone_set('Europe/Amsterdam');
    $date = date("Y-m-d H:i:s");
    $query = "INSERT INTO contr_barcode(booze,barcode,email,datum) VALUES ('$booze','$barcode','$email',$date')";
    if(mysqli_query($con,$query)){
        echo "1";
    }else{
        echo "0";
    }
}

1 个答案:

答案 0 :(得分:0)

您正在发送格式化的值,就像发送application/x-www-form-urlencoded一样,但默认情况下,StringContent会将内容类型声明为text/plain。 PHP框架可能无法正确解析内容,因为它认为您只是发送未格式化的纯文本。

您可以更改StringContent对象的内容类型,也可以使用UrlEncodedFormContent类。例如

    var content = new StringContent(myParameters);
    content.Headers.Content-Type = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    var response = await hc.PostAsync(URI, content);