qq_39955352 2018-12-22 16:01 采纳率: 5.6%
浏览 5994
已结题

后台controller写了个post请求,现在url那边如何接收参数

public static String sendPost(String url, String param) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
//设置网络请求时间最多为5秒;
connection.setConnectTimeout(5000);
//读取网页请求结果时间为15秒
connection.setReadTimeout(25000);
//设置网络请求时间最多为5秒;
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);

connection.setDoInput(true);

//设置请求编码格式
connection.setRequestProperty("Accept-Charset", "GBK");
connection.setRequestProperty("contentType", "GBK");
// 获取URLConnection对象对应的输出流

//文件流编码设置
OutputStreamWriter out = new OutputStreamWriter(connection

.getOutputStream(), "GBK");
out.write(param);

        // flush输出流的缓冲  
        out.flush();  
        System.out.println("进入前....");
        in = new BufferedReader(new InputStreamReader(
                connection.getInputStream(),"UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 使用finally块来关闭输入流
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return result;
} 

//这是我的post请求方法
String url = "http://127.0.0.1:8080/LibrarySyncMdmServer/severe/getPost";
String result=sendPost(url, URLEncoder.encode(context,"UTF-8"));
//这是调用post方法传入的参数

//这是我url的方法,现在我怎么在这个方法里获取,sendPost需要传递的参数 

@Controller
@RequestMapping("/severe")
public class ViewCardNoListServerController extends BaseController {
private static Logger logger = LoggerFactory.getLogger(ViewCardNoListServerController.class);

@RequestMapping("/getPost")
public String getPost(HttpServletRequest request) throws IOException{

    String str1 =request.getParameter("param");

    System.out.println("你已经进入方法。。。。。"+str1);
    return "sucess";
}

}

  • 写回答

6条回答 默认 最新

  • Just Like This ! 2018-12-22 08:14
    关注

    图片说明图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题