亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 C# 客戶端使用 Nodejs Web 服務

通過 C# 客戶端使用 Nodejs Web 服務

C#
人到中年有點甜 2022-10-23 16:18:44
我有一個 web 服務,用 nodejs 編寫并開發 ac# 客戶端。我對 GET 方法沒有任何問題,但對 POST 有問題。這是我的客戶代碼 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = methodType.ToString(); UTF8Encoding encoding = new UTF8Encoding(); if (!string.IsNullOrEmpty(content)) {    Byte[] byteArray = encoding.GetBytes(content);    request.ContentLength = byteArray.Length;    using (var dataStream = request.GetRequestStream())    {       dataStream.Write(byteArray, 0, byteArray.Length);    }  }   request.ContentType = "application/json";   try   {     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())   {     using (var streamReader = new StreamReader(response.GetResponseStream()))      {         var  result = streamReader.ReadToEnd();      }        return response;    }     } .....這是我的服務器代碼router.post('/api/activation/activate', urlencodedParser, (req, res, next) => {    return new Promise(function (resolve, reject) {        (async function () {            try {                routerAppFeature.Delete(req.body.type.toUpperCase(), req.body.id);                console.log(req.body.HardwareId);                console.log(req.params.HardwareId)                console.log(req.query.HardwareId);                var result = { "data": "Okay :)" + req.body.HardwareId, "errorCode": "-1" };                res.json(result);            } catch (error) {                console.log('!!Error in activate method: ' + error);                return reject(error);            }        })();    }).catch(error => {        var err = { "message": error.message, "errorCode": "1" };        res.json(err);    });});所以我的問題是執行using (var dataStream = request.GetRequestStream())后我的 post 方法被觸發并且 req.body 為空,因為在觸發該方法之前我沒有傳遞任何參數..我怎么解決這個問題?
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

我已經解決了這個問題..我仍然不知道真正的問題是什么,但下面的代碼正在工作..


public void Test(string content)

{

  using (var client = new HttpClient())

  {

     try

     {

        var res = client.PostAsync("http://localhost:4000/api/activation/activate",

            new StringContent(content, Encoding.UTF8, "application/json");


       res.Result.EnsureSuccessStatusCode();

       Console.WriteLine("Response " + res.Result.Content.ReadAsStringAsync().Result +

       Environment.NewLine);

     }

     catch (Exception e)

     {

       Console.WriteLine(e.ToString());

     }

   }

 }

所以它與nodejs無關,而是c#方面。


查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號