2012-12-30 53 views
2

我想从我的Java桌面应用程序登录到网站。我一直在使用OutputStreamWriterUsername & Password。我可以从我的应用程序成功登录到其他网站,但不是我真正想要的那个。分析该网站的网页源代码后,我发现,那两个文本框IDUsername & password变化与每一个请求,每次刷新页面。日志从桌面应用程序

<input type="text" name="UserName_88515" id="UserName_88515" /> 

id最后5位数字的每一次变化,所以我决定阅读网页源代码,检索这些五个数字,然后写凭据登录到该网站。

public class LoginHandler { 

static boolean isLoggedIn = false; 
static String responseText, 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(); 
    } 
} 
} 

对于我写了下面的代码,但我得到以下错误:

Cannot write output after reading input 

回答

1

看到了这个问题的答案 - Cannot write output after reading input 短的版本是,底层HttpURLConnection的(康涅狄格州)不能重用,你需要打开一个新的。

普遍的是,它会为宥如果你使用一个更好的库,供您HTTP代码,就像HTTPComponents

+0

我不能这样做,因为,如果你读过我的整个问题,你会发现更容易使用'ID文本框的内容将随新请求再次发生变化。所以如果我打开一个新的连接,我之前检索的ID将无用 –

+0

http连接和http会话之间有区别。你可以打开一个新的连接映射到同一个会话服务器端(这些标识符被保留在那里) – radai

+0

你没有明白我的观点。即使我在同一个会话中“刷新”网页,“ID”的值也会发生变化。 –