您的位置:首页 > 理论基础 > 计算机网络

java代码实现如jsp页面的form请求方式一HttpURLConnection请求

2015-08-24 11:59 274 查看
package com.test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* @title
* @descriptor
* @author zy
* @version 2013-8-26
*/
public class AppAddTest {

public static void main(String[] args) throws Exception {

URL url = new URL("http://127.0.0.1:8080/test/LineToOnline.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
OutputStream out = null;
InputStream in = null;
try {
conn.getOutputStream().write("OAMessage=410000000021".getBytes());
in = conn.getInputStream();
}
catch (Exception e) {
throw e;
}
finally {

if (out != null) {
try {
out.close();
}
catch (Exception e) {
}
}
if (in != null) {
try {
in.close();
}
catch (Exception e) {
}
}
conn.disconnect();
}

}
}

简单的jsp写java代码接受请求

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//String str=new String(request.getParameter("OAMessage").getBytes("ISO8859_1"),"UTF-8");
String str=request.getParameter("OAMessage");
str=URLDecoder.decode(str);
System.out.println(str);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: