Java tutorial
/* * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.roncoo.pay.permission.utils; import java.io.IOException; import java.nio.charset.Charset; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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; /** * httpClientUtils * * www.roncoo.com * * @authorshenjialong */ public class RoncooHttpClientUtils { /** * API * * @param parameters * @return */ @SuppressWarnings({ "resource", "deprecation" }) public static String post(String url, String parameters) { HttpClient httpClient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); String body = null; if (method != null & parameters != null && !"".equals(parameters.trim())) { try { // NameValuePair?? method.addHeader("Content-type", "application/json; charset=utf-8"); method.setHeader("Accept", "application/json"); method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8"))); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return "1";// 1 } // Read the response body body = EntityUtils.toString(response.getEntity()); } catch (IOException e) { // return "2"; } finally { } } return body; } }