We wanted to do a school with httpclient educational system agents landing APP, school connection is http://jwgl2.jmu.edu.cn/Common/Login.aspx, by capturing, finding the target Url To login information and POST Url landing page is the same, and access to the relevant data to pass, first paste the code
public void login () {
String target = "http://jwgl2.jmu.edu.cn/common/login.aspx"; / / destination address to submit
HttpPost httpRequest = new HttpPost (target); / / create HttpPost object
/ / Save the parameters to be passed to the List collection
List params = new ArrayList ();
params.add (new BasicNameValuePair ("__EVENTARGUMENT", ""));
params.add (new BasicNameValuePair ("__EVENTTARGET", ""));
params.add (new BasicNameValuePair ("__LASTFOCUS", "***")) ;/ / *** said to capture the value of catch, the code too, omitted
params.add (new BasicNameValuePair ("__VIEWSTATE", "***"));
params.add (new BasicNameValuePair ("TxtUserName", username)); / / username
params.add (new BasicNameValuePair ("TxtPassword", pwd)); / / password
params.add (new BasicNameValuePair ("TxtVeriCode", code));
params.add (new BasicNameValuePair ("BtnLoginImage.x", "1")) ;/ / 0 for landing when the two press Enter
params.add (new BasicNameValuePair ("BtnLoginImage.y", "1"));
try {
System.out.println ("setHeader before the cookie >>" + MainActivity.cookies.get (0). GetValue ()) ;/ / test cookie values are the same
httpRequest.setHeader ("Cookie", "ASP.NET_SessionId =" + MainActivity.cookies.get (0) getValue ().);
httpRequest.setEntity (new UrlEncodedFormEntity (params, HTTP.UTF_8)); / / set encoding
HttpResponse httpResponse = MainActivity.httpclient
. Execute (httpRequest); / / execute HttpClient request
if (httpResponse.getStatusLine (). getStatusCode () == 200) {
/ / If the request is successful
httpRequest.setHeader ("Cookie", "ASP.NET_SeesionId =" + MainActivity.cookies.get (0). getValue ()) ;/ / get the last POST cookie
login information after completionSystem.out.println ("After the final post of the cookie >>" + MainActivity.cookies.get (0). GetValue ()) ;/ / test
/ * With the cookie can try after a successful landing page opens, the following code is to open a curriculum as a test * /
HttpGet httprs = new HttpGet ("http://jwgl2.jmu.edu.cn/Student/ViewMyStuSchedule.aspx");
cookie httprs.setHeader ("Cookie", "ASP.NET_SessionId =" + MainActivity.cookies.get (0). getValue ()) ;/ / set class schedule page to just get a cookie, I tested with IE landing educational system
/ / And catch the successful landing of cookie, then the value of the value added directly caught on setHeader, you can access the class schedule page and speak html code to play
HttpResponse httpResponse2 = new DefaultHttpClient () execute (httprs);.
if (httpResponse2.getStatusLine (). getStatusCode () == 200) {
StringBuffer sb = new StringBuffer ();
HttpEntity entity = httpResponse2.getEntity ();
InputStream is = entity.getContent ();
BufferedReader br = new BufferedReader (new InputStreamReader (is, "UTF-8"));
/ / Is read to change the encoding of the source, the source format is GB2312, the Anyuan format to read in, and then you want to convert the source code on the line
String data = "";
while ((data = br.readLine ())! = null) {
sb.append (data);
}
result + = sb.toString (); / / At this result is our achievement in the HTML source code
}
else {
result + = "request fails, the return value is:" + httpResponse2.getStatusLine () getStatusCode () ;/ / print out the return code
.}
} Else {
result = "request failed!!";
}
} Catch (UnsupportedEncodingException e1) {
e1.printStackTrace (); / / output error message
} Catch (ClientProtocolException e) {
e.printStackTrace (); / / output error message
} Catch (IOException e) {
e.printStackTrace (); / / output error message
} Finally {
httpRequest.abort ();
}
} Before this last POST is also to get a verification code, as to ensure the effectiveness of the verification code, I was doing before calling login () method first performs a post, and save the cookie POST after, then with This cookie to access the verification code pages. Write a handle inside
public void handleMessage (Message msg) {
if (result! = null) {
Intent intent = getIntent (); / / Get Intent object
Bundle bundle = new Bundle (); / / instantiate transmitted packets
String target = "http://jwgl2.jmu.edu.cn/common/login.aspx"; / / destination address to submit
HttpPost httpRequest_1 = new HttpPost (target); / / create HttpPost object
/ / Save the parameters to be passed to the List collection
List params = new ArrayList ();
params.add (new BasicNameValuePair ("TxtUserName", username)); / / username
params.add (new BasicNameValuePair ("TxtPassword", pwd)); / / password
params.add (new BasicNameValuePair ("BtnLoginImage.x", "1")) ;/ / 0 for landing when the two press Enter
params.add (new BasicNameValuePair ("BtnLoginImage.y", "1"));
try {
httpRequest_1.setEntity (new UrlEncodedFormEntity (params, HTTP.UTF_8)); / / set encoding
HttpResponse httpResponse1 = MainActivity.httpclient
. Execute (httpRequest_1); / / execute HttpClient request
if (httpResponse1.getStatusLine (). getStatusCode () == 200) {
/ / If the request is successful
. MainActivity.cookies = ((AbstractHttpClient) MainActivity.httpclient) getCookieStore () getCookies ();.
System.out.println ("first click to get verification code when the cookie >>" + MainActivity.cookies.get (0) getValue ().);
}
/ * Here is to get the verification code and display * /
in layoutcheckicon = (ImageView) findViewById (R.id.code);
byte [] data = CheckCode.getImageViewArray ();
Bitmap bitmap = BitmapFactory.decodeByteArray (data, 0, data.length);
checkicon.setImageBitmap (bitmap);
} Catch (UnsupportedEncodingException e1) {
e1.printStackTrace (); / / output error message
} Catch (ClientProtocolException e) {
e.printStackTrace (); / / output error message
} Catch (IOException e) {
e.printStackTrace (); / / output error message
} Finally {
httpRequest_1.abort ();
}
}
super.handleMessage (msg);
} Finally, attach the acquisition method validation code,
public static byte [] getImageViewArray () {
byte [] data = null;
InputStream inputStream = null;
/ / Do not need to close the output stream is written directly to memory
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
HttpClient httpclient = new DefaultHttpClient ();
HttpGet httpRequest = new HttpGet (URL_PATH);
HttpResponse httpResponse;
byte [] b_data = new byte [1024];
int len = 0;
try {
System.out.println (MainActivity.cookies.get (0) getValue ().);
httpRequest.setHeader ("Cookie", "ASP.NET_SessionId =" + MainActivity.cookies.get (0) getValue ().);
httpResponse = httpclient.execute (httpRequest);
if (httpResponse.getStatusLine (). getStatusCode () == 200) {
HttpEntity entity = httpResponse.getEntity ();
InputStream is = entity.getContent ();
while ((len = is.read (b_data))! = -1) {
outputStream.write (b_data, 0, len);
}
data = outputStream.toByteArray ();
MainActivity.cookies2 = ((AbstractHttpClient) httpclient) getCookieStore () getCookies ();..
System.out.println ("verification code cookie:" + MainActivity.cookies2);
. / / MainActivity.cookies = ((AbstractHttpClient) MainActivity.httpclient) getCookieStore () getCookies ();.
}
}
catch (ClientProtocolException e) {
e.printStackTrace ();
}
catch (IOException e) {
/ / TODO: handle exception
e.printStackTrace ();
}
return data;
} verification code when I get there setHeader ("cookie", "ASP.NET_SessionId =" + MainActivity.cookies.get (0) getValue ().); Here MainActiviy.cookies is above the handle POST get inside for the first time, it should also be able to verify the validity of the code of a guarantee, but after the last POST finish school, get a password and verification code, access to the authentication cookie is not successful cookie, and and POST and get a Cookie values obtained after the verification code is different. Leading to the final landing failed, finally, I also have been stuck in here, hope to have developed over httpclient successful landing with a verification code predecessors to give pointers. . It will not solve the problem of too uncomfortable! ! !<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->Reply:
Passing Bangding.
This process is validated or your guess. Suggested capture tool to grab the whole process to log down, then references Post httpclient to do with the Post.
Reply:
mark about
Reply:
Give an account grasped package
Reply:
First parse the source code of the login screen, and then extract the parameters and set the cookie, the name value of the parameter sent to the server, manual landing once the url win after a successful login, direct access to it. Just do this procedure. . . Toss for a long time. . . .
Reply:
Just to see you stay off the QQ, but to answer your real name, ah, can not add friends ask? Trouble! 869738395
Reply:
can add QQ? Favor. .
Reply:
I think it should be like this, however, because verify the existence of codes, has been unable to successfully log in, it should be verified code does not match. .
No comments:
Post a Comment