简体   繁体   English

如何使用“ Content-type:application / x-www-form-urlencoded”发出Okhttp请求?

[英]How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”?

I have an api requirement of Sending following parameters in header- 我有一个在标头中发送以下参数的api要求:

Content-Type - application/x-www-form-urlencoded 内容类型-应用程序/ x-www-form-urlencoded

authKey- (Session token) authKey-(会话令牌)

and following parameters in body(form day ie key-value pair) 以及正文中的以下参数(表单日期,即键值对)

storeId -1 storeId -1

type -Product 类型-产品

CategoryId -324 类别编号-324

But whenever I hit this api, I am always getting 401(UnAuthorized) error. 但是,每当我按下此api时,我总是会收到401(UnAuthorized)错误。 I have tried using MultipartRequest body and formBody, I know this is nothing to do with the body(Its the header where I need to send the Content-Type and authKey). 我试过使用MultipartRequest正文和formBody,我知道这与正文无关(它是我需要发送Content-Type和authKey的标头)。 Below is my code- 以下是我的代码-

Request.Builder  requestBuilder = new Request.Builder();
requestBuilder.addHeader("Content-Type", "application/x-www-form-urlencoded");
    requestBuilder.addHeader("authKey",AppSharedPref.getTokenMobikul(context));
RequestBody formbody = new FormBody.Builder().add("CategoryId",bodyparms.get(0)).
                        add("type",bodyparms.get(1)).build();
 requestBuilder.post(formbody);

The Same api is giving Response with retrofit library So how to achieve this using Okhttp. Same api提供了带有改造库的Response,因此如何使用Okhttp实现这一点。

Might this will help 可能会有所帮助

FormBody.Builder formBuilder = new FormBody.Builder()
    .add("key", "value");

// add more parameter as follow:
formBuilder.add("mobile", "9999999999");

RequestBody formBody = formBuilder.build();

Request request = new Request.Builder()
            .url("https://www.hittheserver.com")
            .post(formBody)
            .build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded Spring MVC中内容类型应用程序/ x-www-form-urlencoded的请求参数的顺序 - Order of request parameters for content-type application/x-www-form-urlencoded in Spring MVC 用Java处理Content-Type =“ application / x-www-form-urlencoded”的SOAP请求 - Processing SOAP request with Content-Type = “application/x-www-form-urlencoded” in Java ContentCachingRequestWrapper 只捕获带有 Content-Type:application/x-www-form-urlencoded 的 POST 请求 - ContentCachingRequestWrapper only captures POST request with Content-Type:application/x-www-form-urlencoded 如何编写控制器类以允许内容类型:application / json和application / x-www-form-urlencoded - How to write controller class to allow content-type: application/json and application/x-www-form-urlencoded @JsonProperty 不适用于内容类型:application/x-www-form-urlencoded - @JsonProperty not working for Content-Type : application/x-www-form-urlencoded 在Spring Boot中使用内容类型application / x-www-form-urlencoded的请求的自定义反序列化器 - Custom deserializer for requests with content-type application/x-www-form-urlencoded in Spring Boot 当内容类型为 application/x-www-form-urlencoded 时,Java 读取 POST 数据 - Java read POST data when content-type is application/x-www-form-urlencoded 如何使用@RequestBody注释使内容类型application / x-www-form-urlencoded工作? - How to make the content type application/x-www-form-urlencoded work with @RequestBody annotation? 如何在Struts 2中获取内容类型为x-www-form-urlencoded的参数 - How to get the param which content-type is x-www-form-urlencoded in Struts 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM