- 论坛徽章:
- 0
|
写了一个最简单的demo,用来发起http请求调用。
大家代码里都怎么个搞法?
求深入,求扩展~~- public static void main(String[] args) throws HttpException, IOException {
- testHttp(0);
- testHttp(1);
- }
-
- /**
- * type=0, get
- * type=1, post
- * http://haoma.imobile.com.cn/index.php?mob=18858111994
- */
- public static void testHttp(int type) throws HttpException, IOException{
- HttpClient client = new HttpClient();
- client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");
- HttpMethod method = null;
- switch(type){
- case 0:
- method = getGetMethod();
- break;
- case 1:
- method = getPostMethod();
- break;
- }
-
- client.executeMethod(method);
- System.out.println("statusLine = " + method.getStatusLine());
- String response = new String(method.getResponseBodyAsString());
- System.out.println("response = " + response);
-
- method.releaseConnection();
- }
- private static HttpMethod getGetMethod() {
- return new GetMethod("/index.php?mob=18858111994");
- }
- private static HttpMethod getPostMethod() {
- PostMethod post = new PostMethod("/index.php");
- NameValuePair simcard = new NameValuePair("mob", "18858111994");
- post.setRequestBody(new NameValuePair[] { simcard });
- return post;
- }
复制代码 |
|