在向sd
操作发送请求之前,是否可以添加一些标头?
我想要做的是指定标题:
GET
...
在我的控制器中输入-Accept application/json
-Content-Type application/json
方法之前。
答案 0 :(得分:0)
如果您使用HttpClient发送带有Accept和Content-Type标头的GET请求,则看起来像这样:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri)
{
Content = content,
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
HttpResponseMessage response = await client.SendAsync(request);
请记住,代码使用发送请求的Async版本,因此您必须使用“Async”关键字修饰您的方法才能使其正常工作。
如果你想从前端发送请求,你应该使用ajax,你的代码看起来像这样:
$.ajax({
url: 'URL HERE',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
// here goes the data that came from the response..
}
});
答案 1 :(得分:0)
这个怎么样?
$.ajax({
url: '@Url.Action("GetData", "Home")',
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
}
});
<强>控制器强>
public ActionResult GetData()
{
return Json("What I want to send", JsonRequestBehavior.AllowGet);
}
答案 2 :(得分:0)
您可以使用以下命令:<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PlatformTarget>x64</PlatformTarget>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>
name = input()
with open('filename.txt', 'r') as f:
for line in f.readlines():
if name == line.split(',')[0]:
print(line)