工具类(3)
package com.step.
import java.io.IOE
import java.net.URLD
import java.util.ArrayL
import java.util.L
import org.apache.http.HttpR
import org.apache.http.HttpS
import org.apache.http.NameValueP
import org.apache.http.client.ClientProtocolE
import org.apache.http.client.HttpC
import org.apache.http.client.entity.UrlEncodedFormE
import org.apache.http.client.methods.HttpG
import org.apache.http.client.methods.HttpP
import org.apache.http.entity.StringE
import org.apache.http.impl.client.DefaultHttpC
import org.apache.http.message.BasicNameValueP
import org.apache.http.util.EntityU
import com.alibaba.fastjson.JSONO
* 发送HTTP请求
* @author mlu
public class HttpUtils {
* 发送post请求--用于接口接收的参数为JSON字符串
* @param url 请求地址
* @param params json格式的字符串
public static String httpPost(String url, String params){
String result =
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
* 发送json字符串,这两句需要设置
httpPost.addHeader(&Content-type&,&application/ charset=utf-8&);
httpPost.setHeader(&Accept&, &application/json&);
httpPost.setEntity(new StringEntity(params, &UTF-8&));
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// Read the response body
result = EntityUtils.toString(response.getEntity(),&UTF-8&);
} catch (Exception e) {
e.printStackTrace();
* 发送post请求--用于接口接收的参数为键值对
* @param url 请求地址
* @param nameValuePairs 键值对
public static String httpPost(String url, List&NameValuePair& nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String strResult = &&;
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, &UTF-8&));
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
strResult = EntityUtils.toString(response.getEntity());
// System.out.println(&conResult:&+conResult);
strResult += &发送失败:& + response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return strR
public static String httpGet(String url, List&NameValuePair& nameValuePairs){
HttpClient httpClient = new DefaultHttpClient();
String sb = &&;
String result = &&;
for(NameValuePair nvp:nameValuePairs){
sb += nvp.getName()+&=&+nvp.getValue()+&&&;
sb = sb.substring(0,sb.length()-1);
sb = URLDecoder.decode(sb, &UTF-8&);
HttpGet httpGet = new HttpGet(url+&?&+sb);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
result = EntityUtils.toString(response.getEntity());
result += &发送失败:& + response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
public static void main(String[] args) {
String url = &http://10.140.8.56/gd_fssc/rest/fsscService/getStaffInfo&;
String url2 = &http://localhost:8080/eshore-app-backframe-web/interface/getJson&;
// 发送 POST 请求
JSONObject json = new JSONObject();
json.put(&number&, &@GD&);
httpPost(url,json.toString());
List&NameValuePair& nameValuePairs = new ArrayList&&();
nameValuePairs.add(new BasicNameValuePair(&method&, &login&));
httpGet(url2,nameValuePairs);
引用的jar包:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:125786次
积分:2382
积分:2382
排名:第14659名
原创:124篇
转载:55篇
评论:25条iOS 解决iOS 9下的http请求发送失败问题
来源:博客园
iOS9中 因为系统要求所有的请求都必须使用 https, 所以发送http请求会失败,如果想让程序能够兼容http请求 在info.plist中添加以下代码: 这里需要做的是右键info.plist文件, 显示源代码, 在源代码中进行添加下面这几行代码.
NSAppTransportSecurity
NSAllowsArbitraryLoads
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动