package com.nextjoy.projects.usercenter.util.http; /**
* Created by Administrator on 2016/10/20.
*/
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.net.URLDecoder; public class HttpRequestUtils {
/**
* httpPost
* @param url 路径
* @param jsonParam 参数
* @return
*/
public static String httpPost(String url, String jsonParam){
return httpPost(url, jsonParam, false);
} /**
* post请求
* @param url url地址
* @param jsonParam 参数
* @param noNeedResponse 不需要返回结果
* @return
*/
public static String httpPost(String url, String jsonParam, boolean noNeedResponse){
//post请求返回结果
DefaultHttpClient httpClient = new DefaultHttpClient();
String res = "";
HttpPost method = new HttpPost(url);
try {
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 50000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 50000);
if (null != jsonParam) {
//解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
/**请求发送成功,并得到响应**/
if (result.getStatusLine().getStatusCode() == 200) {
try {
/**读取服务器返回过来的json字符串数据**/
res = EntityUtils.toString(result.getEntity());
if (noNeedResponse) {
return null;
}
} catch (Exception e) {
System.out.println("post请求响应失败:" + url);
}
}
} catch (IOException e) {
System.out.println("post请求提交失败:" + url);
}
return res;
} /**
* 发送get请求
* @param url 路径
* @return
*/
public static String httpGet(String url){
//get请求返回结果
String res = "";
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 50000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 50000);
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request); /**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == 200) {
/**读取服务器返回过来的json字符串数据**/
res = EntityUtils.toString(response.getEntity());
url = URLDecoder.decode(url, "UTF-8");
} else {
System.out.println("post请求提交失败:" + url);
}
} catch (IOException e) {
System.out.println("post请求提交失败:" + url);
}
return res;
} public static void main(String[] args) {
String str = "{\"receipt-data\":\"MIITygYJKoZIhvcNAQcCoIITuzCCE7cCAQExCzAJBgUrDgMCGgUAMIIDawYJKoZIhvcNAQcBoIIDXASCA1gxggNUMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgEDAgEBBAMMATEwCwIBCwIBAQQDAgEAMAsCAQ4CAQEEAwIBeDALAgEPAgEBBAMCAQAwCwIBEAIBAQQDAgEAMAsCARkCAQEEAwIBAzAMAgEKAgEBBAQWAjQrMA0CAQ0CAQEEBQIDAa2yMA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEUDI1MDAYAgEEAgECBBATuifw5XP6GoZIgBahDxWaMBsCAQACAQEEEwwRUHJvZHVjdGlvblNhbmRib3gwHAIBBQIBAQQUstsB Xk3UbkwRJ3bu9Z/okz749kwHgIBDAIBAQQWFhQyMDE4LTA0LTE5VDA4OjU5OjU4WjAeAgESAgEBBBYWFDIwMTMtMDgtMDFUMDc6MDA6MDBaMCACAQICAQEEGAwWam95Lk5leHRqb3lTREtEZW1vLmNvbTA9AgEHAgEBBDXidk/VrGzS8WSU1J0BDdgvRsqRp NrmWJCqzIiTczAUdadruCsIP6aH9wBRxaKXzzpwtcS zBYAgEGAgEBBFA8U1hKBsHQwUWPSU0ukesf8UF/BzYQ3bwsTslxhrNJQ9D94gIhanzhyf5QMBn/JnaWbvR hP6 4p3xcZ0XBWjawPBtq2s6ODN8ZzicKe/ctTCCAVECARECAQEEggFHMYIBQzALAgIGrAIBAQQCFgAwCwICBq0CAQEEAgwAMAsCAgawAgEBBAIWADALAgIGsgIBAQQCDAAwCwIC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I0FwcGxlIFdvcmxkd2lkZSBEZXZlbG9wZXIgUmVsYXRpb25zMUQwQgYDVQQDDDtBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9ucyBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eQIIDutXh eeCY0wCQYFKw4DAhoFADANBgkqhkiG9w0BAQEFAASCAQBFrLcTbjO3f MD5UrL53KS6PuIkCp/JuLkuQN5NDnFiH/KfSbjrIh00Ml c5Gqdbyivw3SnDIEvWhlvHBnr3vYdt1KJig9uqQKbd/FaphptkmSFC/dSlaTKr4OY/jxmMaEkXq2q7i9DLgGAzeS40/oB Gql3hDIBfUUp5Y827mni/PE8KTPVq8qpNgLodcVRd7GjuFu7ABm8M4Ww6cZoTXOg0IvWH1bRQEjMULjFqxHket6jJeYzokjbDk29cbS43IelUUot7Q04q sTIig9ur3bmUUA4mi0sKcnyQO7a4Ul1HE0lMz6TJ THPElkeX1OOG1SeDFPABa10bpn80B5\"}";
String url = "https://sandbox.itunes.apple.com/verifyReceipt";
String result = HttpRequestUtils.httpPost(url, str);
System.out.println(result);
}
}

注:需要httpclient.jar依赖包(这里是版本4.3以上)

HttpRequestUtils post get请求的更多相关文章

  1. JAVA发送HttpClient请求及接收请求结果

    1.写一个HttpRequestUtils工具类,包括post请求和get请求 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

  2. JAVA发送HttpClient请求及接收请求结果过程

    1.写一个HttpRequestUtils工具类,包括post请求和get请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  3. 极光客户互动云java post请求

    package com.common.utils; import java.io.IOException; import java.net.URLDecoder; import java.util.M ...

  4. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  5. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  6. 重温Http协议--请求报文和响应报文

    http协议是位于应用层的协议,我们在日常浏览网页比如在导航网站请求百度首页的时候,会先通过http协议把请求做一个类似于编码的工作,发送给百度的服务器,然后在百度服务器响应请求时把相应的内容再通过h ...

  7. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

  8. nodejs之get/post请求的几种方式

    最近一段时间在学习前端向服务器发送数据和请求数据,下面总结了一下向服务器发送请求用get和post的几种不同请求方式: 1.用form表单的方法:(1)get方法 前端代码: <form act ...

  9. ajax异步请求

    做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school.c ...

随机推荐

  1. hadoop 伪分布配置

    配置 Hadoop 伪分布式 任务配置说明: VMware 15 Centos 6.5 java -jdk 1.8 hadoop-2.6.0-cdh5.14.0.tar.gz 第一步 自行安装虚拟机 ...

  2. 高质量动漫实时画质增强器Anime4K在mpv上的配置

    Anime4K地址 https://github.com/bloc97/Anime4K mpv地址  https://mpv.io/   这个要错峰下载,网速不太好 在C盘用户\..\AppData\ ...

  3. C#多线程(14):任务基础②

    目录 判断任务状态 再说父子任务 组合任务/延续任务 复杂的延续任务 并行(异步)处理任务 并行(同步)处理任务 并行任务的 Task.WhenAny 并行任务状态 循环中值变化问题 定时任务 Tas ...

  4. springboot+dubbo简单分布式RPC调用demo

    使用springboot+dubbo搭建RPC入门案例 本文背景简述: 最近在学习公司的一套RPC框架,初步接触的时候感觉挺复杂的.但是知道其原理肯定是和dubbo很相似的,毕竟都是RPC框架嘛,只是 ...

  5. linux下的.ssh文件夹路径等

    1.linux下的.ssh文件夹在~下,直接cd ~/.ssh即可 2.tp经过gd类处理过的水印图片格式为png 3.前端扒下别人家的网站如果自己本地打开有出现相同的代码段则有可能是js动态添加的, ...

  6. 单源最短路问题--朴素Dijkstra & 堆优化Dijkstra

    许久没有写博客,更新一下~ Dijkstra两种典型写法 1. 朴素Dijkstra     时间复杂度O(N^2)       适用:稠密图(点较少,分布密集) #include <cstdi ...

  7. java 之 继承 super关键籽 this关键字 final关键字

    继承 语法: 使用 extends 来继承 class子类    extends父类{ 子类属性 子类方法 } 继承的特点: 1.子类会把父类所有的属性和方法继承下来,final修饰的类是不可以被继承 ...

  8. certutil 导入 CA 证书

    2019独角兽企业重金招聘Python工程师标准>>> 在linux下使用GoAgent客户端的时候,需要导入CA.cer证书. 安装证书管理工具 apt-get install l ...

  9. P1495 CRT,P4777 EXCRT

    updata on 2020.4.11 修正了 excrt 的一处笔误 CRT 求解方程: \[\begin{cases} x \equiv a_1 \pmod {m_1}\\ x \equiv a_ ...

  10. 最新Idea超实用告别996插件,都是免费

    Idea告别996插件 在IntelliJ IDEA中,秉着IDEA自带能实现的快捷方式就不用插件的原则,少用些插件,运行性能也提升一些,虽然很少,哈哈.分享下我个人常用的插件,希望对大家有些帮助.插 ...