HTTP提交数据
1 /**
2 * 將放入的參數轉換成POST的形式key=value
3 * @param parames
4 * @param encode
5 * @return
6 */
7 public static String sendPostMessage(Map<String,String> parames,String encode){
8
9 StringBuffer buffer=new StringBuffer();
10 try{
11
12 if(parames!=null&&!parames.isEmpty())
13 {
14 for(Map.Entry<String, String> entry:parames.entrySet()){
15 buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encode)).append("&");
16 }
17 }
18 return sendOutput(buffer.toString());
19 }catch(Exception e){
20
21 }
22 return null;
23
24 }
25 /**
26 * @param outputStream 傳入的要輸出的對象的流
27 * @param data 獲取發送的數據
28 * @return
29 */
30 private static String sendOutput(String data){
31 try{
32 byte[] mydata=data.getBytes();
33 URL url=new URL(URL_path);
34 HttpURLConnection connection=(HttpURLConnection)url.openConnection();
35 connection.setConnectTimeout(3000);
36 connection.setDoInput(true);
connection.setDoOutput(true);
//設置提交數據方式
connection.setRequestMethod("POST"); 37 //設置手機端訪問服務器端時請求頭信息(http協議) 38 //設置請求的類型為文本類型 39 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 40 //設置請求體的長度 41 connection.setRequestProperty("Content-Length", String.valueOf(mydata.length)); 42 43 OutputStream outputStream=connection.getOutputStream(); 44 outputStream.write(mydata); 45 46 return changeInputStream(connection.getInputStream(), "utf-8"); 47 48 }catch(Exception e){ 49 50 } 51 return null; 52 } 53 54 /** 55 * 強輸入流轉換為字符串輸出到內存 56 * @param inputStream 57 * @param encode 58 * @return 59 */ 60 private static String changeInputStream(InputStream inputStream,String encode){ 61 ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); 62 byte[] buffer=new byte[1024]; 63 int length=0; 64 try { 65 while(-1!=(length=inputStream.read(buffer))){ 66 outputStream.write(buffer, 0, length); 67 } 68 return new String(outputStream.toByteArray(), "encode"); 69 } catch (IOException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 74 return null; 75 } 76 public static void main(String[] args) { 77 Map<String, String> parames=new HashMap<String,String>(); 78 parames.put("username", "admin"); 79 parames.put("password", "admin"); 80 sendPostMessage(parames, "utf-8"); 81 } 82 }
connection.setDoOutput(true);
//設置提交數據方式
connection.setRequestMethod("POST"); 37 //設置手機端訪問服務器端時請求頭信息(http協議) 38 //設置請求的類型為文本類型 39 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 40 //設置請求體的長度 41 connection.setRequestProperty("Content-Length", String.valueOf(mydata.length)); 42 43 OutputStream outputStream=connection.getOutputStream(); 44 outputStream.write(mydata); 45 46 return changeInputStream(connection.getInputStream(), "utf-8"); 47 48 }catch(Exception e){ 49 50 } 51 return null; 52 } 53 54 /** 55 * 強輸入流轉換為字符串輸出到內存 56 * @param inputStream 57 * @param encode 58 * @return 59 */ 60 private static String changeInputStream(InputStream inputStream,String encode){ 61 ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); 62 byte[] buffer=new byte[1024]; 63 int length=0; 64 try { 65 while(-1!=(length=inputStream.read(buffer))){ 66 outputStream.write(buffer, 0, length); 67 } 68 return new String(outputStream.toByteArray(), "encode"); 69 } catch (IOException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 74 return null; 75 } 76 public static void main(String[] args) { 77 Map<String, String> parames=new HashMap<String,String>(); 78 parames.put("username", "admin"); 79 parames.put("password", "admin"); 80 sendPostMessage(parames, "utf-8"); 81 } 82 }
?
轉載于:https://www.cnblogs.com/oldcownotGiveup/p/5403983.html
總結
- 上一篇: OpenGLES 2.0 可编程渲染管线
- 下一篇: 理解Storm Metrics