1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public static String httpPost(String url,String json) { String res = ""; OkHttpClient okHttpClient = new OkHttpClient().newBuilder().cookieJar(new CookieJar() { private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>(); @Override public void saveFromResponse(HttpUrl httpUrl, List<Cookie> cookies) { cookieStore.put(httpUrl.host(), cookies); } @Override public List<Cookie> loadForRequest(HttpUrl httpUrl) { List<Cookie> cookies = cookieStore.get(httpUrl.host()); return cookies != null ? cookies : new ArrayList<Cookie>(); } }).connectTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).build(); RequestBody requestBody = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); try { Response response=okHttpClient.newCall(request).execute(); res = response.body().string(); } catch (IOException e) { e.printStackTrace(); } return res; }
|