1 package caicai.web; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.net.httpURLConnection; 8 import java.net.URL; 9 import java.net.URLEncoder; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 import org.apache.http.HttpResponse; 15 import org.apache.http.NameValuePair; 16 import org.apache.http.client.ClientProtocolException; 17 import org.apache.http.client.HttpClient; 18 import org.apache.http.client.entity.UrlEncodedFormEntity; 19 import org.apache.http.client.methods.HttpGet; 20 import org.apache.http.client.methods.HttpPost; 21 import org.apache.http.client.utils.URLEncodedUtils; 22 import org.apache.http.impl.client.DefaultHttpClient; 23 import org.apache.http.message.BasicNameValuePair; 24 import org.apache.http.util.EntityUtils; 25 26 public class service { 27 28 public static string get_save(String name, String phone) { 29 /** 30 * 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码 31 * tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式 32 */ 33 String path = "http://192.168.0.117/testxml/web.php"; 34 Map<String, String> params = new HashMap<String, String>(); 35 try { 36 params.put("name", URLEncoder.encode(name, "UTF-8")); 37 params.put("phone", phone); 38 return sendgetrequest(path, params); 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 return "提交失败"; 44 } 45 46 private static String sendgetrequest(String path, Map<String, String> params) 47 throws Exception { 48 49 // path="http://192.168.0.117/testxml/web.php?name=xx&phone=xx"; 50 StringBuilder url = new StringBuilder(path); 51 url.append("?"); 52 for (Map.Entry<String, String> entry : params.entrySet()) { 53 url.append(entry.getKey()).append("="); 54 url.append(entry.getValue()).append("&"); 55 } 56 url.deleteCharAt(url.length() - 1); 57 URL url1 = new URL(url.toString()); 58 HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); 59 conn.setConnectTimeout(5000); 60 conn.setRequestMethod("GET"); 61 if (conn.getResponseCode() == 200) { 62 InputStream instream = conn.getInputStream(); 63 ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 64 byte[] buffer = new byte[1024]; 65 while (instream.read(buffer) != -1) { 66 outstream.write(buffer); 67 } 68 instream.close(); 69 return outstream.toString().trim(); 70 71 } 72 return "连接失败"; 73 } 74 75 public static String post_save(String name, String phone) { 76 /** 77 * 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码 78 * tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式 79 */ 80 String path = "http://192.168.0.117/testxml/web.php"; 81 Map<String, String> params = new HashMap<String, String>(); 82 try { 83 params.put("name", URLEncoder.encode(name, "UTF-8")); 84 params.put("phone", phone); 85 return sendpostrequest(path, params); 86 } catch (Exception e) { 87 // TODO Auto-generated catch block 88 e.printStackTrace(); 89 } 90 return "提交失败"; 91 } 92 93 private static String sendpostrequest(String path, 94 Map<String, String> params) throws Exception { 95 // name=xx&phone=xx 96 StringBuilder data = new StringBuilder(); 97 if (params != null && !params.isEmpty()) { 98 for (Map.Entry<String, String> entry : params.entrySet()) { 99 data.append(entry.getKey()).append("="); 100 data.append(entry.getValue()).append("&"); 101 } 102 data.deleteCharAt(data.length() - 1); 103 } 104 byte[] entiy = data.toString().getBytes(); // 生成实体数据 105 URL url = new URL(path); 106 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 107 conn.setReadTimeout(5000); 108 conn.setRequestMethod("POST"); 109 conn.setDoOutput(true);// 允许对外输出数据 110 conn.setRequestProperty("Content-Type", 111 "application/x-www-form-urlencoded"); 112 conn.setRequestProperty("Content-Length", String.valueOf(entiy.length)); 113 OutputStream outstream = conn.getOutputStream(); 114 outstream.write(entiy); 115 if (conn.getResponseCode() == 200) { 116 InputStream instream = conn.getInputStream(); 117 ByteArrayOutputStream byteoutstream = new ByteArrayOutputStream(); 118 byte[] buffer = new byte[1024]; 119 while (instream.read(buffer) != -1) { 120 byteoutstream.write(buffer); 121 } 122 instream.close(); 123 return byteoutstream.toString().trim(); 124 } 125 return "提交失败"; 126 } 127 128 public static String httpclient_postsave(String name, String phone) { 129 /** 130 * 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码 131 * tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式 132 */ 133 String path = "http://192.168.0.117/testxml/web.php"; 134 Map<String, String> params = new HashMap<String, String>(); 135 try { 136 params.put("name", name); 137 params.put("phone", phone); 138 return sendhttpclient_postrequest(path, params); 139 } catch (Exception e) { 140 // TODO Auto-generated catch block 141 e.printStackTrace(); 142 } 143 return "提交失败"; 144 } 145 146 private static String sendhttpclient_postrequest(String path,Map<String, String> params) { 147 List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 148 for (Map.Entry<String, String> entry : params.entrySet()) { 149 pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 150 } 151 try { 152 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairs,"UTF-8"); 153 HttpPost httpost=new HttpPost(path); 154 httpost.setEntity(entity); 155 HttpClient client=new DefaultHttpClient(); 156 HttpResponse response= client.execute(httpost); 157 if(response.getStatusLine().getStatusCode()==200){ 158 159 return EntityUtils.toString(response.getEntity(), "utf-8"); 160 } 161 } catch (Exception e) { 162 // TODO Auto-generated catch block 163 e.printStackTrace(); 164 } 165 166 return null; 167 } 168 169 170 public static String httpclient_getsave(String name, String phone) { 171 /** 172 * 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码 173 * tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式 174 */ 175 String path = "http://192.168.0.117/testxml/web.php"; 176 Map<String, String> params = new HashMap<String, String>(); 177 try { 178 params.put("name", name); 179 params.put("phone", phone); 180 return sendhttpclient_getrequest(path, params); 181 } catch (Exception e) { 182 // TODO Auto-generated catch block 183 e.printStackTrace(); 184 } 185 return "提交失败"; 186 } 187 188 private static String sendhttpclient_getrequest(String path,Map<String, String> map_params) { 189 List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); 190 for (Map.Entry<String, String> entry : map_params.entrySet()) { 191 params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 192 } 193 String param=URLEncodedUtils.format(params, "UTF-8"); 194 HttpGet getmethod=new HttpGet(path+"?"+param); 195 HttpClient httpclient=new DefaultHttpClient(); 196 try { 197 HttpResponse response=httpclient.execute(getmethod); 198 if(response.getStatusLine().getStatusCode()==200){ 199 return EntityUtils.toString(response.getEntity(), "utf-8"); 200 } 201 } catch (ClientProtocolException e) { 202 // TODO Auto-generated catch block 203 e.printStackTrace(); 204 } catch (IOException e) { 205 // TODO Auto-generated catch block 206 e.printStackTrace(); 207 } 208 return null; 209 } 210 211 }