【发布时间】:2022-01-27 01:40:50
【问题描述】:
我有一个正在运行的 web 服务,我只有一个 api 调用,它运行良好。
所以现在我想添加另一个 api 但我无法让这个工作,当我在浏览器中尝试时它一直返回这个错误
请求包含实体主体,但没有 Content-Type 标头。这 不支持推断的媒体类型“application/octet-stream” 这个资源
如果我在我的 testclient c# 项目中尝试它,它会返回 Method not allowed
我所做的与已经工作的 api 完全相同,那么这个令人讨厌的错误的原因是什么?
来自工作 API 的代码
public class SCSController : ApiController
{
// GET: SCS
public HttpResponseMessage AddDriverPayments(DriverPayment driverPayments)
{
HttpResponseMessage result = null;
try
{
DriverPaymentResponse response = new DriverPaymentResponse() { PaymentID = driverPayments.newID, SCS_ID = driverPayments.sCS_ID };
result = Request.CreateResponse(response);
}
catch (Exception ex)
{
var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("error"),
ReasonPhrase = ex.Message
};
throw new HttpResponseException(resp);
}
return result;
}
来自不工作的 api 的代码
public class VLAController : ApiController
{
// GET: VLA
public HttpResponseMessage GetAppLogin(AppLoginRequest appLoginRequest)
{
HttpResponseMessage result = null;
try
{
//VLADataBase db = new VLADataBase();
//AppLoginResponse response = db.GetAppLogin(appLoginRequest);
AppLoginResponse response = new AppLoginResponse() { Authorized = true, TruckID = 123 };
result = Request.CreateResponse(response);
}
catch (Exception ex)
{
var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("error"),
ReasonPhrase = "fout: " + ex.Message
};
throw new HttpResponseException(resp);
}
return result;
}
我如何在我的测试客户端中调用它
这就是我调用工作 api 的方式
string JsonData = JsonConvert.SerializeObject(payments);
System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
try
{
var response = await client.PostAsync(comboBoxEditWebService.Text, restContent);
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStringAsync();
DriverPaymentResponse Result = JsonConvert.DeserializeObject<DriverPaymentResponse>(stream);
textBoxResult.Text = Result.SCS_ID + " " + Result.PaymentID;
}
else
{
textBoxResult.Text = response.StatusCode + " " + response.ReasonPhrase;
}
}
catch (Exception ex)
{
textBoxResult.Text = ex.Message;
}
这就是我调用不工作的 api 的方式
string JsonData = JsonConvert.SerializeObject(appLoginRequest);
System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
try
{
var response = await client.PostAsync(comboBoxEdit1.Text, restContent);
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStringAsync();
AppLoginResponse Result = JsonConvert.DeserializeObject<AppLoginResponse>(stream);
textBox1.Text = Result.Authorized.ToString() + " " + Result.TruckID.ToString(); //Result.SCS_ID + " " + Result.PaymentID;
}
else
{
textBox1.Text = response.StatusCode + " " + response.ReasonPhrase;
}
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
我必须注意,两个 api 都在不同的单元中,在不同的控制器中,这可能是个问题吗?
那么我怎样才能解决这个问题,以便我可以同时使用这两个 api 呢?
【问题讨论】:
-
您的端点是什么 HTTP 方法?我没有看到它在任何地方定义。但是,您在这两种方法中调用
client.PostAsync()来调用 API,这表明两者都定义为 POST 方法。但是GetAppLogin-name 暗示它可能是一个 GET 方法?这也可以解释为什么它说“现在允许方法”。 -
@thesystem
GetAppLogin只是一个名称,并不意味着它使用GET方法。我还没有开始这个项目,也没有使用 web 服务的经验。如何查看方法是如何定义为 POST 或 GET 的? -
@thesystem 另外不要忘记,一种方法已经运行了一年多没有问题,所以我猜它是 POST。但我找不到这是在哪里设置的
-
对不起,我有点困惑它是如何工作的。在这两种方法中,您都创建了一个完全空的 HttpClient:
HttpClient client = new HttpClient();。至少,我认为您必须指定必须将 HttpClient 调用定向到的位置,例如设置基地址 (docs.microsoft.com/en-us/dotnet/api/…)。我认为该项目有一个非常不寻常的配置(或者更有可能是我不熟悉的配置)。通常,控制器端点用例如[HttpGet]或[HttpPost]装饰。网络服务可能不同 -
@thesystem 用
[HttpPost]装饰api 成功了,谢谢。如果您将此作为答案发布,我会接受它
标签: c# api rest web-services