發起http請求raw格式
阿新 • • 發佈:2019-01-06
週末加班調別人給的介面,總是他的介面報錯500。
但是用postman呼叫,設定引數格式為raw的json格式就可以通了,自己的java程式碼就是不通
人家又不加班 自己百度了看看。
查到了好辦法
真是變態,必須要這種拼接成的json字串,直接把map給toString就不行。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; }