Web Services 一个API返回:发送Web服务请求时不允许使用方法

fv2wmkja  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(205)

我有一个工作的webservice运行在那里,我只有一个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的?

jtoj6r0c

jtoj6r0c1#

[HttpPost]来修饰端点就成功了(见评论中的对话)。

相关问题