Http请求两种方式
来源:互联网 发布:java 视频服务器 编辑:程序博客网 时间:2024/05/18 00:40
方式一:
/** * 发送https请求 * * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 * @return 返回微信服务器响应的信息 */@Deprecatedpublic String httpsRequest(String requestUrl, String requestMethod, String outputStr) { String resContent = null; DefaultHttpClient httpClient = new DefaultHttpClient (); try { // 创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = new org.apache.http.conn.ssl.SSLSocketFactory(sslContext); ssf.setHostnameVerifier(new AllowAllHostnameVerifier ()); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme ("https", ssf, 443)); HttpPost httpGet = new HttpPost (requestUrl); httpGet.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"); httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded"); httpGet.setEntity(new StringEntity (outputStr, "UTF-8")); HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { resContent = EntityUtils.toString(httpEntity);// 取出应答字符串 resContent = new String(resContent.getBytes("ISO-8859-1"), "UTF-8"); httpEntity.consumeContent(); } } else { httpGet.abort(); } } catch (Exception e) { log.error("连接超时:{}", e); } finally { httpClient.getConnectionManager().shutdown(); } return resContent;}
方式二:
/** * 请求,只请求一次,不做重试 * * @param url 请求的地址 * @param data 提交的数据 * @param connectTimeoutMs 连接超时时间 * @param readTimeoutMs 读取超时时间 * @return * @throws Exception */ public String requestOnce(String url, String data, int connectTimeoutMs, int readTimeoutMs) throws Exception { BasicHttpClientConnectionManager connManager; //设置连接管理器,BasicHttpClientConnectionManager对象线程安全,每次只管理一个连接 connManager = new BasicHttpClientConnectionManager ( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(), null, null, null ); //构建客户端 HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connManager) .build(); HttpPost httpPost = new HttpPost(url); //设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build(); httpPost.setConfig(requestConfig); //设置请求头和请求数据编码格式 StringEntity postEntity = new StringEntity(data, "UTF-8"); httpPost.addHeader("Content-Type", "text/xml"); httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"); httpPost.setEntity(postEntity); //发送请求,接收请求数据,将请求数据转为string HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); return EntityUtils.toString(httpEntity, "UTF-8"); }
阅读全文
0 0
- Http请求两种方式
- 发送HTTP请求的两种方式
- Android的http两种请求方式
- HTTP请求之两种方式
- http 请求 两种主要的请求方式
- Http两种请求
- C# wnform 请求http ( get , post 两种方式 )
- android之Http两种post请求方式
- C# winform 请求http ( get , post 两种方式 )
- C# wnform 请求http ( get , post 两种方式 )
- 访问Http接口的两种请求方式
- Java发送Http请求的两种方式java.net.URLConnection(方式一)
- Java发送Http请求的两种方式HttpClient(方式二)
- http--八种请求方式
- HTTP-8种请求方式
- HTTP的两种提交请求的方式get和post
- http协议的两种主要请求方式post和get
- http消息当中,post和get两种请求方式的区别
- Unity使用GL动态画点
- Python学习---20
- 慕课网《HTML+CSS基础课程》笔记总结(四)
- Unreal fbx导入问题总结
- unbutu忘记登录密码
- Http请求两种方式
- MySQL字符集详解
- RenderScript 让你的Android计算速度快的飞上天!
- mysql的utf8编码配置
- bootstrap-fileinput组件上传
- linux上的一个玩具
- Android存储-SharedPreferences
- Java覆盖equals方法
- 工厂模式及使用反射实现工厂模式