我想做个J2SE登陆到WEB的程序,但是登陆帐号密码核对完后,我就被WEB程序给T回了程序,我登陆的地方是后台,我想实现用J2SE对WEB后台操作,请不要帖下面的代码:URL url = new URL("http://localhost/admin/chklogin.asp");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream raw = conn.getOutputStream();
OutputStream buf = new BufferedOutputStream(raw);
OutputStreamWriter out = new OutputStreamWriter(buf,"GBK");
out.write("admin=admin&password=admin");
out.flush();
out.close();
conn.connect();
InputStream in=conn.getInputStream();
in=new BufferedInputStream(in);
Reader r=new InputStreamReader(in);
int c;
//int i=0;
byte[] b=new byte[1800];
int j=0;
while(true)
{
int i=in.read();
if(i==-1) break;
b[j++]=(byte)i;
}
System.out.println(new String(b));这段代码,就是我用过的。如果我登陆失败,会跳到登陆失败的界面,成功后转入admin.asp<%
if session("aleave")="" then
response.redirect "adminlogin.asp"
response.end
end if
%>我就被这代码T回来的。密码核对成功后转到admin.asp,这是admin.asp的头部代码session("admin")=rs("admin")
session("password")=rs("password")
session("aleave")=rs("aleave")
response.redirect "admin.asp"但是到了admin.asp我会被T回来希望大家解决一下,我高分酬谢
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream raw = conn.getOutputStream();
OutputStream buf = new BufferedOutputStream(raw);
OutputStreamWriter out = new OutputStreamWriter(buf,"GBK");
out.write("admin=admin&password=admin");
out.flush();
out.close();
conn.connect();
InputStream in=conn.getInputStream();
in=new BufferedInputStream(in);
Reader r=new InputStreamReader(in);
int c;
//int i=0;
byte[] b=new byte[1800];
int j=0;
while(true)
{
int i=in.read();
if(i==-1) break;
b[j++]=(byte)i;
}
System.out.println(new String(b));这段代码,就是我用过的。如果我登陆失败,会跳到登陆失败的界面,成功后转入admin.asp<%
if session("aleave")="" then
response.redirect "adminlogin.asp"
response.end
end if
%>我就被这代码T回来的。密码核对成功后转到admin.asp,这是admin.asp的头部代码session("admin")=rs("admin")
session("password")=rs("password")
session("aleave")=rs("aleave")
response.redirect "admin.asp"但是到了admin.asp我会被T回来希望大家解决一下,我高分酬谢
http://httpunit.sourceforge.net/
做你要的这个功能很简单就可以实现
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class TestHTTP {
public TestHTTP() {
}
public static void main(String[] argv) throws Exception {
URL url = new URL("http://localhost/admin/CHKLOGIN.ASP");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); // encode the message
String name = "admin="+URLEncoder.encode("admin","UTF-8");
String email = "password="+URLEncoder.encode("admin","UTF-8"); // send the encoded message
out.println(name+"&"+email);
out.close();
java.util.Map map=connection.getHeaderFields();
java.util.Iterator it=map.keySet().iterator();
while(it.hasNext()) {
Object key=it.next();
System.out.println("key="+key+" value="+map.get(key));
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
//System.out.println(line);
}
System.out.println("----------------------");
in.close();
connection.setFollowRedirects(true);
String cookie=connection.getHeaderField("Set-Cookie").toString().substring(0,connection.getHeaderField("Set-Cookie").toString().indexOf(";"));
url=new URL("http://localhost/admin/CHKLOGIN.ASP");
connection=(HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie",cookie);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
out = new PrintWriter(connection.getOutputStream());
name = "admin="+"admin";
email = "password="+"admin"; // send the encoded message
out.println(name+"&"+email);
System.out.println(name+"&"+email);
out.flush();
out.close();
System.out.println(connection.getRequestProperty("Cookie"));
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
我现在代码是这样的,但是还是不行
我在浏览器打开url+数据都登录不了,也不知道是什么安全机制
表示关注,建议重发帖子,分高点
或者试试把url改成
url=new URL("http://localhost/admin/CHKLOGIN.ASP?admin="+admin+"&password="+password);
* 处理进程
*/
public String process(String phone, String pwd, String songid) throws
SAXException,
MalformedURLException, IOException {
info1.setText("正在登陆");
URL url = null;
String v = "";
WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse(loginPage.getText()); String str = wr.getText();
int start = str.indexOf("<input type=\"text\" style=\"width:40\" name=\"randompwd\" value=\"\" maxlength=\"6\" class=\"input-style2\" >") +
"<input type=\"text\" style=\"width:40\" name=\"randompwd\" value=\"\" maxlength=\"6\" class=\"input-style2\" >".
length();
int end = str.indexOf("</td>", start);
str = str.substring(start, end).trim();
String[] validate1 = str.split(".gif\">"); for (int i = 0; i < validate1.length; i++) {
validate1[i] = validate1[i].trim();
v = v +
validate1[i].substring(validate1[i].length() - 1,
validate1[i].length());
}
WebForm wf = wr.getForms()[0];
String p[] = wf.getParameterNames(); WebRequest request = wf.getRequest();
request.setParameter("areanumber", "0731");
request.setParameter("callnumber", phone);
request.setParameter("password", pwd);
request.setParameter("randompwd", v);
String s = "document.inputForm.usernumber = '"+ quhao.getText() + phone + "';";
wr.getScriptableObject().doEvent(s);
s = "document.inputForm.cardpass = '" + pwd + "';";
wr.getScriptableObject().doEvent(s);
s = "document.inputForm.ipaddr = '" + randomIP() + "';";
wr.getScriptableObject().doEvent(s);
s = "document.inputForm.onlyflag = '0';";
wr.getScriptableObject().doEvent(s);
wf.submit(); info1.setText("正在处理");
try {
Thread.sleep(400);
} catch (InterruptedException ex1) {
} wr = wc.getCurrentPage(); String in = wr.getText();
if (in.indexOf("用户不存在") != -1 || in.indexOf("您输入的密码不正确") != -1) {
info1.setText("用户名或者密码不正确");
try {
Thread.sleep(400);
} catch (InterruptedException ex1) {
}
return "用户名或者密码不正确";
}
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
HttpUnitOptions.getScriptErrorMessages();
wr = wc.getResponse(
"http://ring.hnphs.net/colorring/buyring.action?ringidtype=1&ringid=" +
songid + "&grouptype=-1&scorebuy=0");
in = wr.getText();
if (in.indexOf("用户已经拥有这条铃音") != -1) {
info1.setText("用户已经拥有这条铃音");
try {
Thread.sleep(400);
} catch (InterruptedException ex1) {
}
//删除铃声
info1.setText("正在删除用户已经订购的铃声");
wr = wc.getResponse("http://ring.hnphs.net/colorring/initeditring.action");
s = "document.personringlibtable.action='/colorring/delpersonring.action?ringid="+songid+"&ringidtype=1';";
wr.getScriptableObject().doEvent(s);
wf = wr.getFormWithName("personringlibtable");
wr = wf.submit();
return "铃声已经删除";
} info1.setText("铃音订购成功");
try {
Thread.sleep(400);
} catch (InterruptedException ex1) {
}
return "铃音订购成功";
} /**
* 随机产生一个IP地址
* @return String 产生的IP地址字符串
*/
public String randomIP() {
Random rand = new Random();
String str = "";
for (int i = 0; i < 4; i++) {
str = str + "." + (rand.nextInt(20) + 225);
}
str = str.substring(1, str.length());
System.out.println(str);
return str;
} /**
* 弹出一个消息提示
* @param msg String 要显示的内容
*/
public void showMessage(String msg) {
jButton1.setEnabled(true);
fileNameField.setEditable(true);
jButton2.setEnabled(true);
jButton2.setText("开始");
JOptionPane.showMessageDialog(null, msg); }
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}这个我是写的一个登陆网站并进行了一个操作的代码,用HttpUnit实现的