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;
@Controller
public 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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106279.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...