资料来源:网络整理
时间:2023/2/14 0:52:05 共计:3602 浏览
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace c_http2.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpPost]
public IActionResult GetResult()
{
Request.ContentType = "application/json";
var str = new StreamReader(Request.Body);
var data_text = str.ReadToEnd();
JObject data = JsonConvert.DeserializeObject<JObject>(data_text);
string key1 = HttpContext.Request.Headers["key1"];
string key2 = HttpContext.Request.Headers["key2"];
data.Add("key1", (JToken)key1);
data.Add("key2", (JToken)key2);
return Ok(data);
}
[HttpGet]
public IActionResult ReturnResult()
{
var url = "http://localhost:98/api/Test/GetResult";
string str_postdata = JsonConvert.SerializeObject(new { a = "a123", b = "b234", t = "1212c567" });
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=utf-8";
//request.ContentLength = Encoding.UTF8.GetByteCount(str_postdata);
request.Headers.Add("key1", "key1");
request.Headers.Add("key2", "key2");
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8"));
myStreamWriter.Write(str_postdata);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
JObject bupo = JsonConvert.DeserializeObject<JObject>(retString);
myStreamReader.Close();
myResponseStream.Close();
return Ok(bupo);
}
}
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。