`
wking_forever
  • 浏览: 61573 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Sample---simulate the http request

 
阅读更多
package com.wlq;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class RDRHttpCallSample {

	private static final int TIMEOUT = 50000;
	
    public final static String BAIDU_URL = "http://www.baidu.com" ;
	
    public final static String MSG_NULL = "content is null";

	
	public static boolean validate(String source) {
		boolean rs = true;
		if (source.equals(MSG_NULL))
			rs = false;
		if (source.indexOf("ERROR#") != -1 || source.indexOf("ErrorMsg") != -1 || source.indexOf("ERROR_TYPE") != -1)
			rs = false;
		return rs;
	}

	public static String getHttpUsefulInfo(StringBuffer buffer) {

		String rs = "";
		if (buffer.indexOf("<body>") != -1) {
			int index = buffer.indexOf("<body>");
			buffer.replace(0, index + 6, "");
			index = buffer.indexOf("</body>");
			buffer.replace(index, buffer.length(), "");
		}
		rs = buffer.toString();
		return rs.equals("") ? rs = MSG_NULL : rs;
	}
	
	public static StringBuffer doCall(String url, String queryStr){
		
		StringBuffer rs = new StringBuffer();
		HttpURLConnection conn = null;
		String sTotalString = "";
		int respCode = 0;
		try{
			URL console = new URL(url);
			conn = (HttpURLConnection) console.openConnection();
			conn.setConnectTimeout(TIMEOUT);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			
			conn.getOutputStream().write(queryStr.getBytes());
			conn.getOutputStream().flush();
			conn.getOutputStream().close();
			conn.connect();
			
			respCode = conn.getResponseCode();
			if(respCode == 200){
				String sCurrentLine = "";
				InputStream is = conn.getInputStream();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is));
				while ((sCurrentLine = reader.readLine()) != null)
					if (sCurrentLine.length() > 0)
						sTotalString = sTotalString + sCurrentLine.trim();
			}else{
				sTotalString = "Call the rdr server error, error code is " + respCode;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn !=null)
				conn.disconnect();
			if(validate(sTotalString))
				rs.append(getHttpUsefulInfo(new StringBuffer(sTotalString)));
		}
		return rs;
	}
	
	public enum QueryStrGenerator{
		
				BAIDU_CASE{
			@Override
			public String getQueryStr(){
				String str ="s?wd=what+if+you+go&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&inputT=2094";
				return str;
			}
		};
		abstract public String getQueryStr();
	}
	
	public static void main(String...args){
		
		String url = BAIDU_URL,
			   queryStr = QueryStrGenerator.BAIDU_CASE.getQueryStr();
		StringBuffer rs = doCall(url, queryStr);
		System.out.println(rs.toString());
	}
}
分享到:
评论

相关推荐

    ExpRing----A Tool To Simulate The Fragment of Brittle Materials

    脆性材料碎裂模拟在工程领域中具有广泛的应用,特别是在地质力学、材料科学以及结构工程中,对于理解和预测材料在受力状态下的破坏行为至关重要。"ExpRing"是一款专为此目的设计的工具,它能够有效地模拟脆性材料的...

    A three-dimensional multi-scale method to simulate the ion transport behavior

    A three-dimensional multi-scale method to simulate the ion transport behavior of cement-based materials

    onenet虚拟设备调试工具sandbox_simulate-device2.2.3

    标题中的“onenet虚拟设备调试工具sandbox_simulate-device2.2.3”指的是中国移动OneNet平台提供的一个模拟物联网(IoT)设备的调试工具。这个工具的版本为2.2.3,它允许开发者在不实际操作物理硬件的情况下,仿真...

    jquery-simulate-ext:jQuery模拟扩展

    jQuery Simulate Extended插件(又名jquery-simulate-ext)提供了基于插件来模拟复杂用户交互的方法。 该插件提供以下功能的仿真: 拖放 按键顺序 按键组合 此外,扩展插件包括jQuery模拟插件本身的文档和修补程序...

    gx simulate6-7.rar

    gx simulate6-7rar,gx simulate6-7

    gx simulate6-4.rar

    gx simulate6-4rar,gx simulate6-4

    前端开源库-express-simulate-latency

    【Express-Simulate-Latency】是一个专门为前端开发者设计的开源库,主要用于在开发环境中模拟网络延迟,以帮助测试和优化应用在不同网络条件下的性能。这个库是基于Node.js的Express框架构建的,可以方便地集成到...

    前端项目-jquery-simulate.zip

    这个压缩包的核心是`jquery-simulate-master`目录,它很可能包含了源码、示例、文档等相关资源。 在Web开发中,模拟事件是一种常见的测试和调试技术。例如,开发者可能需要在不实际交互的情况下触发某些DOM元素的...

    gx simulate6-9.rar

    gx simulate6-9rar,gx simulate6-9

    miniprogram-simulate:小程序自定义组件测试工具集

    miniprogram-simulate 介绍 小程序自定义组件测试工具集。 目前因为小程序独特的运行环境,所以对于小程序自定义组件的单元测试一直没有比较优雅的解决方案,此工具集就是为了解决此痛点而诞生的。将原本小程序...

    gx simulate6-8.rar

    gx simulate6-8rar,gx simulate6-8

    gx simulate6-5.rar

    gx simulate6-5rar,gx simulate6-5

    gx simulate6-3.rar

    gx simulate6-3rar,gx simulate6-3

    cordova-simulate:在浏览器中模拟 Apache Cordova 应用程序

    安装 npm install -g cordova-simulate用法命令行界面从 Cordova 项目中的任何地方的命令行,输入以下内容: simulate [] [--device=] [--corsproxy=] [--dir=] [--livereload=] [--port=] [--forceprepare=] [--...

    jquery-simulate:jQuery事件单元测试助手

    jQuery.simulate() 模拟事件以帮助单元测试用户交互。项目状态jQuery Foundation的项目正在使用jquery-simulate,但并未进行积极的开发。 通常,问题会在受影响时由jQuery UI团队的成员解决,而其他拉取请求会持续...

    Android-Simulate.rar

    "Android-Simulate"项目就是这样一个工具,它实现了在PC上运行Android应用,利用OpenGL ES 1.1来模拟图形渲染,为开发者提供了一个便捷的测试平台。 一、OpenGL ES 1.1简介 OpenGL ES是OpenGL的一个子集,专为...

    Paper-Replicate-Learning-to-Simulate-Equity-Option-Markets

    深层对冲:学习模拟股票期权市场欢迎来到我们的论文复制项目-的作者:罗松和张云shu。 本文分为七个部分。 他们是: 问题描述现有方法的缺点探索性数据分析甘斯模型结果与评估缺点和挑战未来研究问题描述将强化学习...

    ns-2 extension to simulate localization system in wireless sensor networks.pdf

    根据提供的文件信息,本文将对“ns-2 extension to simulate localization system in wireless sensor networks”这一主题进行详细解读。文档主要介绍了如何在NS-2(Network Simulator version 2)网络仿真器中添加...

Global site tag (gtag.js) - Google Analytics