当前位置: 首页 > 知识库问答 >
问题:

如何从Android向C#web API发送post请求

汤念
2023-03-14
    public class TestController : ApiController
    {
        // POST: api/test
        [HttpPost]
        [Route("api/test")]
        public void Post()
        {
            string param = Request.Content.ReadAsStringAsync().Result;
            EventLogger.writeErrorLog("Posted payload --> " + param)
        }
   }
private void postDummy() {
    String url = "http://10.0.2.2:1106/api/test";

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JSONObject jsonBodyObj = new JSONObject();

    try{
        jsonBodyObj.put("payload", "XYZ");
    }catch (JSONException e){
        e.printStackTrace();
    }
    final String requestBody = jsonBodyObj.toString();
    Log.d("Json :--> ", requestBody);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            url, null, new Response.Listener<JSONObject>(){
        @Override    public void onResponse(JSONObject response) {
            Log.i("Response",String.valueOf(response));
        }
    }, new Response.ErrorListener() {
        @Override    public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
        }
    }){
        @Override
        public String getBodyContentType() {
            return "application/json";
        }

        @Override
        public byte[] getBody() {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                        requestBody, "utf-8");
                return null;
            }
        }
    };

    requestQueue.add(jsonObjectRequest);
}
D/Json :-->: {"payload":"XYZ"}

Postman测试结果Web API测试结果截图

共有1个答案

范修伟
2023-03-14

我的情况非常相似,当我从android volley调用时,C#web API返回400(自定义头)。解决方案(在我的例子中)很简单,只是删除了“content-type”“application/json”,让volley做它所做的任何事情。

我的代码工作:Android:

                try{
                JSONObject postparams = new JSONObject();
                postparams.put("param1", "1");
                postparams.put("param2", "2");
                String URL=sesion.urlAire + "api/adjunto/getArchivo";
                RequestQueue requestQueue= Volley.newRequestQueue(context);
                JsonObjectRequest objectRequest=new JsonObjectRequest(
                    Request.Method.POST,
                    URL,
                    postparams,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject  response) {
                            //do something
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //do something
                        }
                    }
                )
                {
                    @Override
                    public Map getHeaders() throws AuthFailureError {
                        HashMap headers = new HashMap();
                        //headers.put("Content-Type", "application/json");
                        headers.put("authorization", sesion.token);
                        return headers;
                    }
                };
                requestQueue.add(objectRequest);

            }catch (Exception e){}

C#:

    [HttpPost]
    public CustomObject getArchivo(PostData postData) {
        return new CustomObject{ param1="1" };
    }
 类似资料:
  • 问题内容: 我正在使用下面的代码发送http POST请求,该请求将对象发送到WCF服务。可以,但是如果我的WCF服务还需要其他参数怎么办?如何从Android客户端发送它们? 这是我到目前为止编写的代码: 问题答案: 使用POST发布参数: 你错过的部分在以下内容中,即如下。 其余的事情你都可以做到。

  • 我在网上找到了这个脚本: 但我不明白如何与PHP一起使用它,也不明白params变量内部的内容是什么,也不明白如何使用它。我能帮个忙吗?

  • POST请求发送json数据java HTTPURLConnection 从Android发送JSON HTTP POST请求 Java-通过POST方法轻松发送HTTP参数 应用程序/x-www-form-urlencoded还是多部分/form-data? 如何使用POST向HttpURLConnection添加参数 JSON解析,创建URLConnection-Android Studio

  • 我想从php发送post请求到python并得到答案,我写了这个脚本发送post 我使用定制python服务器

  • 从Excel电子表格执行HTTP POST需要什么VBA代码?

  • 问题内容: 我想将我的网址发送到(和)。 我当前的请求代码不起作用。 使用是 不是 一个容易回答。 问题答案: 在iOS中发送和请求非常容易。无需其他框架。 请求: 首先,将我们的(按需发送的内容)创建为,然后将其转换为。 目标 接下来,我们阅读的,因此我们可以将其传递给请求。 现在我们有了要发布的内容,我们可以创建一个,并包含我们的。 迅速 最后,我们可以发送请求,并通过创建新的请求来阅读回复:

  • 我正在使用Volley从我的android应用程序向运行在http://192.168.1.4:3000/Battery_Signal_Report中的本地服务器发送一个带有参数的http post请求。我很确定服务器运行正常(我用Postman成功地检查了它)。 此外,我使用ip 10.0.2.2成功地通过Android Studio的模拟器发送了请求 为了使它正常工作,我使用了各种请求实现,包

  • 请求方式: "|3|2|url,content|\r" 参数: url 设置Post请求的url链接 content post请求的数据 返回值: "|3|code|data|\r" 参数: code http请求返回的成功或者错误码 成功:code = 200 获取数据失败:code = -1 http请求字段错误:code = 1 data http请求返回的数据 Arduino样例: sof