永发信息网

java http post 怎么设置 raw格式

答案:3  悬赏:70  手机版
解决时间 2021-03-23 11:55
  • 提问者网友:嘚啵嘚啵
  • 2021-03-23 06:29
java http post 怎么设置 raw格式
最佳答案
  • 五星知识达人网友:北方的南先生
  • 2019-12-21 04:14
调试微信推广支持中二维码生成api的接口,使用chrome浏览器的postman插件,post请求时有一个选项是form-data,或者raw,使用raw可以请求成功,from-data不知道怎么组装key和value所以一直失败。非常不明白raw是什么意思,google百度都没有相关的解释。后来研究发现,其实raw方式使用的是纯字符串的数据上传方式,所以在POST之前,可能需要手工的把一些json/text/xml格式的数据转换成字符串,是一种post原始请求,区别于form-data这种常用的key-value方式。
public static String result; public static void httpTest() throws ClientProtocolException, IOException { String token = "R5amyr6NyXCtWdScmNiuvVwBCJztfByZDUGaE2V0NwOUheW4XYlvUusYkrViTYt584RgcyXRhjxAJZG3rFlPLg"; String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token; String json = "{"action_name":"QR_LIMIT_SCENE","action_info":{"scene":{"scene_id":234}}}"; HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); StringEntity postingString = new StringEntity(json);// json传递 post.setEntity(postingString); post.setHeader("Content-type", "application/json"); HttpResponse response = httpClient.execute(post); String content = EntityUtils.toString(response.getEntity()); // Log.i("test",content); System.out.println(content); result = content; }

以上代码中需要导入

import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils;

Android中自带org.apache.http相关库文件,所以可以快捷键(ctrl+shift+o)一次导入成功。
全部回答
  • 1楼网友:街头电车
  • 2020-09-29 09:42
服务器端对提交的数据的具体要求是怎样的 ~ ~
  • 2楼网友:行路难
  • 2020-01-18 23:13
public static String result; public static void httpTest() throws ClientProtocolException, IOException { String token = "R5amyr6NyXCtWdScmNiuvVwBCJztfByZDUGaE2V0NwOUheW4XYlvUusYkrViTYt584RgcyXRhjxAJZG3rFlPLg"; String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token; String json = "{\"action_name\":\"QR_LIMIT_SCENE\",\"action_info\":{\"scene\":{\"scene_id\":234}}}"; HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); StringEntity postingString = new StringEntity(json);// json传递 post.setEntity(postingString); post.setHeader("Content-type", "application/json"); HttpResponse response = httpClient.execute(post); String content = EntityUtils.toString(response.getEntity()); // Log.i("test",content); System.out.println(content); result = content; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯