这是一个普通的java测试类,测试可以打印出sessionID
@Test public void test() { String userName = "****"; String password = "*** OutputStream os; HttpURLConnection con; String sessionID = ""; URL realUrl; try { realUrl = new URL("http://seam.ustb.edu.cn:8080/jwgl/Login"); con = (HttpURLConnection) realUrl.openConnection(); //设置请求超时时间 con.setReadTimeout(20000); con.setConnectTimeout(20000); // 设置POST方法 con.setRequestMethod("POST"); con.setUseCaches( false ); // 设置请求头 con.setRequestProperty("Accept", "text/html"); con.setRequestProperty("Connection", "keep-alive"); con.setInstanceFollowRedirects(false); // 禁止响应3**类型重定向 // 设置允许输入输出流 con.setDoInput(true); con.setDoOutput(true); String str = "username=" + userName + "&password=" + password + "&usertype=student"; con.connect(); os = con.getOutputStream(); // 把提交数据以输出流的方式写到服务器 os.write(str.getBytes()); os.flush(); os.close(); con.disconnect(); sessionID = con.getHeaderField("Set-Cookie"); sessionID = sessionID.substring(0, sessionID.indexOf(";")); System.out.println(sessionID); } catch (Exception e) { e.printStackTrace(); } }
而同样一段代码放在androi应中时却无法访问,设置按钮点击事件,开启子线程进行登录验证,没有响应。
这个是activity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initData(); login.setOnClickListener(new OnClickListener() { public void onClick(View v) { LoginAsyncTask l = new LoginAsyncTask(); l.execute("41356021","zhRU19950804@"); } }); } private void initData() { username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.Login); }
这个是验证登录线程
public class LoginAsyncTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } @Override protected String doInBackground(String... params) { String userName = params[0]; String password = params[1]; OutputStream os; HttpURLConnection con; String sessionID = ""; URL realUrl; try { realUrl = new URL("http://seam.ustb.edu.cn:8080/jwgl/Login"); con = (HttpURLConnection) realUrl.openConnection(); //设置请求超时时间 con.setReadTimeout(20000); con.setConnectTimeout(20000); // 设置POST方法 con.setRequestMethod("POST"); con.setUseCaches( false ); // 设置请求头 con.setRequestProperty("Accept", "text/html"); con.setRequestProperty("Connection", "keep-alive"); con.setInstanceFollowRedirects(false); // 禁止响应3**类型重定向 // 设置允许输入输出流 con.setDoInput(true); con.setDoOutput(true); String str = "username=" + userName + "&password=" + password + "&usertype=student"; con.connect(); os = con.getOutputStream(); // 把提交数据以输出流的方式写到服务器 os.write(str.getBytes()); os.flush(); os.close(); con.disconnect(); sessionID = con.getHeaderField("Set-Cookie"); sessionID = sessionID.substring(0, sessionID.indexOf(";")); Log.e("s", sessionID); } catch (Exception e) { e.printStackTrace(); } return sessionID; } }
这里的sessionID为null,求高手指导
解决方案
40
應該是太早disconnect吧, 把doInBackground的代碼換下面的看看
OutputStream os = null; HttpURLConnection con = null; try { String userName = params[0]; String password = params[1]; String sessionID; URL realUrl = new URL("http://seam.ustb.edu.cn:8080/jwgl/Login"); con = (HttpURLConnection) realUrl.openConnection(); //设置请求超时时间 con.setReadTimeout(20000); con.setConnectTimeout(20000); // 设置POST方法 con.setRequestMethod("POST"); con.setUseCaches(false); // 设置请求头 con.setRequestProperty("Accept", "text/html"); con.setRequestProperty("Connection", "keep-alive"); con.setInstanceFollowRedirects(false); // 禁止响应3**类型重定向 // 设置允许输入输出流 con.setDoInput(true); con.setDoOutput(true); String str = "username=" + userName + "&password=" + password + "&usertype=student"; con.connect(); os = con.getOutputStream(); // 把提交数据以输出流的方式写到服务器 os.write(str.getBytes()); os.flush(); sessionID = con.getHeaderField("Set-Cookie"); sessionID = sessionID.substring(0, sessionID.indexOf(";")); Log.v("s", sessionID); } catch (IOException ex) { Log.e("s", "Error", ex); } finally { if (con != null) { con.disconnect(); } if (os != null) { try { os.close(); } catch (IOException ex) { Log.e("s", "Error", ex); } } }