import net.sf.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
public class HttpClient {
public static Object post(String address, JSONObject inputParameters) {
return post(address, inputParameters, Object.class);
}
public static <T> T post(String address, JSONObject inputParameters, Class<T> className) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
String jsonStr = inputParameters == null ? "" : inputParameters.toString();
HttpEntity<String> entity = new HttpEntity<String>(jsonStr, headers);
return restTemplate.postForObject(address, entity, className);
}
}
public JSONObject getJDToken(String code) throws JdException{
String url = “https://oauth.jd.com/oauth/token?"
+ “grant_type=authorization_code&”
+ “client_id=”+config.getJDAppKey()+”&"
+ “client_secret=”+config.getJDAppSecret()+"&"
+ “scope=read&state=12345”+"&"
+ “code=”+code+"&"
+ “redirect_uri=”+config.getJdRedirectUri();
String resultStr = HttpClient.post(url, null, String.class);
JSONObject result = JSONObject.fromObject(resultStr);
return result;
}