实现客户给的URL接口,以爬虫的方式

一、Post方式
public static String sendPost(String url, String param)
			throws RuntimeException {
		StringBuffer result = new StringBuffer();
		HttpURLConnection httpConn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		try {
			URL httpurl = new URL(url);
			httpConn = (HttpURLConnection) httpurl.openConnection();
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			out = new PrintWriter(httpConn.getOutputStream());

			out.print(param);
			out.flush();
			out.close();
			// 采用UTF-8编码,解决中文乱码
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), ENCODED));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			in.close();
		} catch (Exception e) {
			System.err.println("post方式发送失败,URL=" + url + ",params=" + param
					+ e.getMessage());
			LOG.error("post方式发送失败,URL=" + url + ",params=" + param
					+ e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (httpConn != null) {
				httpConn.disconnect();
			}
			if (out != null) {
				out.close();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result.toString();
	}
/**
	 * GET方式发送
	 * 
	 * @param url
	 * @param param
	 * @return
	 */
	public static String sendGet(String url, String param) throws RuntimeException {
		StringBuffer result = new StringBuffer();
		BufferedReader in = null;
		try {
			String urlName = url + "?" + param;//
			URL U = new URL(urlName);
			URLConnection connection = U.openConnection();
			connection.connect();
			// 采用UTF-8编码,解决中文乱码
			in = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), ENCODED));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			in.close();
		} catch (Exception e) {
			System.err.println("get方式发送失败,URL="+url+",params="+param+e.getMessage());
			LOG.error("get方式发送失败,URL="+url+",params="+param+e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result.toString();
	}
/**
	 * POST方式发送
	 * @param url
	 * @param param
	 * @return
	 */
	public static InputStream sendPostToInputStream(String url, String param) throws RuntimeException{
		StringBuffer result = new StringBuffer();
		HttpURLConnection httpConn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		try {
			URL httpurl = new URL(url);
			httpConn = (HttpURLConnection) httpurl
					.openConnection();
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			out = new PrintWriter(httpConn.getOutputStream());
			out.print(param);
			out.flush();
			out.close();
			// 采用UTF-8编码,解决中文乱码
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(),ENCODED));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			in.close();
			return new ByteArrayInputStream(result.toString().getBytes());
		} catch (Exception e) {
			System.err.println("post方式发送失败,URL="+url+",params="+param+e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (httpConn != null) {
				httpConn.disconnect();
			}
			if (out != null) {
				out.close();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
/**
	 * GET方式发送
	 * 
	 * @param url
	 * @param param
	 * @return
	 */
	public static InputStream sendGetToInputStream(String url, String param) throws RuntimeException{
		StringBuffer result = new StringBuffer();
		BufferedReader in = null;
		try {
			String urlName = url + "?" + param;//
			URL U = new URL(urlName);
			URLConnection connection = U.openConnection();
			connection.connect();
			// 采用UTF-8编码,解决中文乱码
			in = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), ENCODED));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			in.close();
			return new ByteArrayInputStream(result.toString().getBytes());
		} catch (Exception e) {
			System.err.println("get方式发送失败,URL="+url+",params="+param+e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
/**
	 * httpClient post发送json
	 * @author Mosnter
	 * @param url
	 * @param json
	 * @return
	 */
	public static JSONObject post(String url, String json) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		try {
			StringEntity s = new StringEntity(json);
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.getContentCharSet(entity);
				response = JSONObject.fromObject(result);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return response;
	}
/**
	 * 获取用户真实IP
	 *
	 * @author cnpayd 
	 * @param request
	 * @return
	 */
	public static String getRealIp(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值