【问题标题】:Using a client to POST data and accepting that Data through WCF POST method使用客户端 POST 数据并通过 WCF POST 方法接受该数据
【发布时间】:2017-02-17 08:04:39
【问题描述】:

我正在尝试使用我的客户端将 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";
    }

【问题讨论】:

    标签: c# json server client


    【解决方案1】:

    我会说它在你的 UriTemplate 中,你不应该在那里使用 {} 参数。您通过 POST 的正文发送它。 我无法测试它,但这是我的第一个猜测。

    【讨论】:

    • 我已经把参数拿出来了。当我运行它时,我收到此错误:说我正在使用:“GET /Service1.svc/doPost”,当我假设正在使用 post
    • Application Insights 遥测(未配置):{"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2017-02-17T19:16:24.7684792Z","tags" :{"ai.operation.name":"GET /Service1.svc/doPost","ai.operation.id":"JlJUJw/8Sdw=","ai.location.ip":"::1"," ai.cloud.roleInstance":"DESKTOP-0U0ICHR","ai.internal.sdkVersion":"web:2.2.0-738"},"data":{"baseType":"RequestData","baseData":{ "ver":2,"id":"JlJUJw/8Sdw=","name":"GET /Service1.svc/doPost","duration":"00:00:00.0050000","success":false," responseCode":"405","url":"localhost:4686/Service1.svc/doPost","properties":{"DeveloperMode":"true"}}}}
    • 这只是当我运行服务器而不是客户端时
    猜你喜欢
    • 2019-02-24
    • 2017-11-19
    • 1970-01-01
    • 2013-05-21
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多