post请求百度网址内容和访问本地springmvc工程controller

来源:互联网 发布:linux wine qq 编辑:程序博客网 时间:2024/06/11 08:21

1,访问百度

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;public class SendPost {public static void main(String[] args) throws Exception{// TODO Auto-generated method stub PrintWriter out = null;    BufferedReader in = null;    String result = "";    try {        URL realUrl = new URL("http://www.baidu.com");        URLConnection conn = realUrl.openConnection();        conn.setConnectTimeout(5000);        conn.setReadTimeout(10*1000);        conn.setDoOutput(true); // 发送POST请求必须设置如下两行        conn.setDoInput(true);        out = new PrintWriter(conn.getOutputStream());        out.flush();        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));        String line;        while ((line = in.readLine()) != null) {            result += line;            System.out.println(line);        }    } catch (Exception e) {        throw e; // 异常外抛    } finally{        try{            if(out!=null)out.close();            if(in!=null) in.close();        }        catch(Exception ex){        }    }}}

2,访问本地启动的springmvc工程的controller

import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicHeader;import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSONObject;import com.allinfinance.cis.credit.api.bean.PrdReq;import com.sun.tools.javac.util.List;public class TestPost2 {public static void main(String[] args) throws Exception{// TODO Auto-generated method stub//1,创建httpClient对象HttpClient client = new HttpClient();//2.创建httpPost对象PostMethod postMethod = new PostMethod("http://localhost:8080/springMVC2/view");postMethod.addParameter("aa", "--------------");//3,设置一个list,以键值对存放请求内容//List list=null;//list.add(1,"aa");//list.add(2,"bb");//4,将对象包装成一个Entity对象//StringEntity entity=new UrlEncodedFormEntity(,"utf-8");//5,设置请求内容//httpPost.setEntity(entity);//6,设置请求报文头部编码//httpPost.setHeader(new BasicHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"));//7,设置期望返回值编码//httpPost.setHeader(new BasicHeader("Accept","test/plain;charset=utf-8"));//8,执行post请求//9,获取响应码int statusCode=client.executeMethod(postMethod);//int statusCode=response.getStatusLine().getStatusCode();//10,如果响应码等于200 说明返回请求成功if(statusCode==200){//11,,获取数据String rs = postMethod.getResponseBodyAsString();//12,输出System.out.println("请求成功,输出结果"+rs);}else{//13,输出System.out.println("请求失败!");}}}


springmvc工程的controller类:

package com.springmvc.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class ViewController {    @RequestMapping("/view")    public ModelAndView view(HttpServletRequest request,String aa){        ModelAndView mav = new ModelAndView();        String contextPath=request.getContextPath();        mav.addObject("context",contextPath);        mav.setViewName("index");        System.out.println("SSSSSSSSSSSSSSSSSSSss"+aa);        return mav;    }}

springmvc工程的下载地址:http://download.csdn.net/download/csdnliuxin123524/10001431


原创粉丝点击