- 直接上代码:这里的submitDataByDoGet(Map<String, String> map, String path) 如何调用??
- public class SubmitDataByHttpClientAndOrdinaryWay {
- /**
- * 使用get请求以普通方式提交数据
- * @param map 传递进来的数据,以map的形式进行了封装
- * @param path 要求服务器servlet的地址
- * @return 返回的boolean类型的参数
- * @throws Exception
- */
- public Boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception {
- // 拼凑出请求地址
- StringBuilder sb = new StringBuilder(path);
- sb.append("?");
- for (Map.Entry<String, String> entry : map.entrySet()) {
- sb.append(entry.getKey()).append("=").append(entry.getValue());
- sb.append("&");
- }
- sb.deleteCharAt(sb.length() - 1);
- String str = sb.toString();
- System.out.println(str);
- URL Url = new URL(str);
- HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection();
- HttpConn.setRequestMethod("GET");
- HttpConn.setReadTimeout(5000);
- // GET方式的请求不用设置什么DoOutPut()之类的吗?
- if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- return true;
- }
- return false;
- }
- 直接给出调用实例就行
复制代码
|
|