Java デスクトップ アプリケーションから Web サイトにログインしたいのですが、主な問題は、要求ごとにIDが変わることです。textbox
<input type="text" maxlength="10" name="UserName_88515" id="UserName_88515" />
ここで id は まで同じまま_
で、次の 5 桁はリクエストごとに変わります。そこで、最初に Web ページを読み、5 桁を取得することにしました。次に、Webサイトにログインするためのデータを書き込もうとしました。しかし、上記のエラーが発生しています。助けてください。
public class LoginHandler {
static boolean isLoggedIn = false;
static String toastText, myText;
public void login(String usrname, String password, String cookys, String sessionCode) {
try {
URL url = new URL("http://www.somewebsite.com/home.php?session="
+ sessionCode);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
conn.setRequestProperty("Cookie", cookys);
conn.setDoOutput(true);
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuilder response = new StringBuilder();
myText = response.toString();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
final String text1 = response.toString();
final int starIndex = text1.indexOf("UserName_");
final int endIndex = starIndex + 15;
System.err.println("This is starIndex" + starIndex);
System.err.println("This is endIndex" + endIndex);
final String avc = text1.substring(starIndex, endIndex);
System.err.println("This is avc\n" + avc);
final String fin = avc.substring(10, 15);
System.err.println("This is fin\n" + fin);
final String data1 = URLEncoder.encode("MessageLength", "UTF-8")
+ "=" + URLEncoder.encode("140", "UTF-8") + "&"
+ URLEncoder.encode("UserName_" + fin, "UTF-8") + "="
+ URLEncoder.encode("username", "UTF-8") + "&"
+ URLEncoder.encode("Password_" + fin, "UTF-8") + "="
+ URLEncoder.encode("password", "UTF-8") + "&"
+ URLEncoder.encode("LoginNowbtnDiv", "UTF-8") + "="
+ URLEncoder.encode("Login Now", "UTF-8") + "&"
+ URLEncoder.encode("LoginNow", "UTF-8") + "="
+ URLEncoder.encode("Login Now", "UTF-8");
System.err.println("THIS IS Data1:\n " + data1);
final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data1);
wr.flush();
final BufferedReader rd1 = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuilder response1 = new StringBuilder();
String line1;
while ((line1 = rd1.readLine()) != null) {
response1.append(line1);
}
final String text2 = response1.toString();
System.err.println("This is second response\n" + text2);
rd1.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
編集
SEVERE: java.net.ProtocolException: Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1083)
at mypack.UltooSender.send(UltooSender.java:80)
at mypack.NewServlet.processRequest(NewServlet.java:41)
at mypack.NewServlet.doGet(NewServlet.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
SEVERE: at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1083)
SEVERE: at mypack.UltooSender.send(UltooSender.java:80)
SEVERE: at mypack.NewServlet.processRequest(NewServlet.java:41)
SEVERE: at mypack.NewServlet.doGet(NewServlet.java:63)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
SEVERE: at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE: at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE: at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE: at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE: at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE: at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE: at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE: at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE: at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE: at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE: at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE: at java.lang.Thread.run(Thread.java:722)