使用客户端POST数据并通过WCF POST方法接受该数据

时间:2017-02-17 08:04:40

标签: c# json server client

我正在尝试使用我的客户端将base64String数据发送到使用WCF的服务器。我在双方的Post方法上遇到了麻烦。我似乎无法接收任何内容或处理发送的任何数据。

客户端:

  private void On_FingerDetect(object sender, FingerPrintArgs e)
    {
        if (!button1.Enabled)
        {
            byte[] newPrint = e.getPrint();
            string print = Convert.ToBase64String(newPrint);
            string json = JsonConvert.SerializeObject(new KeyValuePair<string, string>("print", print));

            // Post URL here
            PostAsync("http://localhost:4686/Service1.svc/doPost/", json);

            toolStripStatusLabel1.Text = "Processing Finger Print";
            //toolStripStatusLabel1.Text = print;
        }
    }
    public async Task PostAsync(string uri, string data)
    {
        var httpClient = new HttpClient();

        var response = await httpClient.PostAsync(uri, new StringContent(data));

        var content = response.Content;

        await content.ReadAsStringAsync();

        button1.Enabled = true;
        toolStripStatusLabel1.Text = data;
    }

服务器端:

 [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "doPost/{value}")]
    [OperationContract]
    string doPost(string value);


public class Service1 : IService1
{

    static string myConnection = "DSN=MS Access Database";
    OdbcConnection myConn = new OdbcConnection(myConnection);
    OdbcCommand mycommand;
    OdbcTransaction transaction;
    OdbcDataAdapter adapter;

    public string doPost(string value)
    {
        // Trace.WriteLine("hello", value);
        byte[] b = Convert.FromBase64String(value);
        OdbcConnection myConn = new OdbcConnection(myConnection);
        string query = "INSERT INTO fingerPrintDb (fingerPrint) VALUES ('" + value + "')";
        OdbcCommand mycommand = new OdbcCommand(query, myConn);

        myConn.Open();
        transaction = myConn.BeginTransaction();

        mycommand.Connection = myConn;
        mycommand.Transaction = transaction;
        mycommand.CommandText = query;

        mycommand.ExecuteNonQuery();
        transaction.Commit();
        myConn.Close();

        return "Success";
    }

1 个答案:

答案 0 :(得分:0)

我说你的UriTemplate,你不应该在那里使用{} parm。你是通过POST机构发送的。 我无法测试,但这是我的第一次猜测。