http post/get 2种使用方式
2015-10-27 18:05
363 查看
public class HttpUtil { //HttpPost public static String executePost(String url, List<NameValuePair> list) { HttpPost post = new HttpPost(url); // HttpDelete // post.addHeader("Authorization", "MDowMDExMTExMTExMTE="); // 认证token // post.addHeader("Content-type", "text/xml;charset=UTF-8"); // post.addHeader("Connection", "keep-alive"); if (list != null && list.size() > 0) { try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8); post.setEntity(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(post); // response.getHeaders("Authorization"); ????????????????????token? if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity()); return result; } else { return "-2"; } } catch (Exception e) { e.printStackTrace(); } return "-2"; } //HttpGet public static String executeGet(String url, Map<String, String> params, String encoding) throws Exception { // 使用StringBuilder对象 StringBuilder sb = new StringBuilder(url); sb.append('?'); // 迭代Map for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()).append('=').append( URLEncoder.encode(entry.getValue(), encoding)).append('&'); } sb.deleteCharAt(sb.length() - 1); // 打开链接 URL url2 = new URL(sb.toString()); HttpGet get = new HttpGet(url2); // HttpDelete // get .addHeader("Authorization", "MDowMDExMTExMTExMTE="); // 认证token // get .addHeader("Content-type", "text/xml;charset=UTF-8"); // get .addHeader("Connection", "keep-alive"); // HttpParams params = new BasicHttpParams(); // ConnManagerParams.setTimeout(params, 20000); // get.setParams(params); HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity()); return result; } else { return "-2"; } } catch (Exception e) { e.printStackTrace(); } return "-2"; } HttpURLConnection 方式: ===================== //HttpURLConnection get public void sendSms() throws Exception{ String message="货已发到"; message=URLEncoder.encode(message, "UTF-8"); System.out.println(message); String path ="http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+message; URL url =new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5*1000); conn.setRequestMethod("GET"); InputStream inStream = conn.getInputStream(); byte[] data = StreamTool.readInputStream(inStream); String result=new String(data, "UTF-8"); System.out.println(result); } HttpURLConnection方式: //HttpURLConnection post public static String uploads(String xml, String path, String authorization) { try { byte[] data = xml.getBytes("UTF-8"); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(8 * 1000); conn.setReadTimeout(8 * 1000); conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); conn.setRequestMethod("POST"); // Post方式 conn.setRequestProperty("connection", "keep-alive"); // 客户端到服务器端的连接持续有效 conn.setRequestProperty("Authorization", authorization); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); StringBuffer buf = new StringBuffer(); String s = ""; if (conn.getResponseCode() == 200) { InputStream in = conn.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(in)); while ((s = br.readLine()) != null) { buf.append(s); } br.close(); in.close(); } return buf.toString(); } catch (Exception e) { e.printStackTrace(); } return "-2"; } }
相关文章推荐
- 网络营销广告的类型(CPC、CPM、CPA、CPS, CTR)
- 卷积神经网络中卷积概念的理解
- iOS网络请求---1
- linux socket 设置从哪个网络设备发送数据 SO_BINDTODEVICE
- android 判断当前网络是否可用
- 通过URLHttpConnection方式连接网络步骤,获取位图为例
- Tomcat部署成Https模式的方法
- 【UFLDL】多层神经网络
- Pawn Storm网络间谍行动再度现身
- 【iOS】使用 Reachability 第三方库 检测网络
- 使用TCP传输从客户端上传文件到服务器端
- tcp三次握手、socket编程
- TCP/IP
- Android判断网络是否连接
- 计算机基础--网络基础专项练习笔记
- 深入理解HTTP Session
- HTTP协议的作用原理
- Java之网络爬虫WebCollector+selenium+phantomjs(二)
- sockaddr,sockaddr_in,sockaddr_un结构体详细讲解
- linux服务器TCP并发连接数