我尝试使用JAVA中的HttpURLConnection类在Skype中登录我的用户。我的目标是为我的用户获取Auth令牌(每24小时过期)。我跟踪每个HTTP呼叫中发送的请求cookie,并在JAVA程序中发送相同的cookie集。但是当收到响应时,通过浏览器和HttpURLConnection类登录时,HTTP调用收到的cookie会有所不同。
有人可以帮忙吗?
/**
* Created by shreyas on 23/09/15.
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HttpsURLConnection {
public static void main(String[] args) throws Exception{
String httpsURL = "https://login.skype.com/login?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com&intsrc=client-_-webapp-_-production-_-go-signin&message=logged_out&lc=16393";
String query = "username="+URLEncoder.encode("username","UTF-8");
query += "&";
query += "password="+URLEncoder.encode("password","UTF-8") ;
String cookies = "MSFPC=ID=7257d650ce6da54f8f16951c796b637f&CS=3&LV=201508&V=1; SWACC=TM=1442036652; tracking=1443005674353; mbox=PC#1441957179127-901122.28_13#1445597676|session#1443005606468-117694#1443007536|check#true#1443005736; frmrgtpe=1-3; lastLogin=SK; skype-session=5a490f691979c9d9cfc9b592d8efe2b3828feb29; skype-session-token=ed386369f29ac162d028d11fe4d6f365b462ba1c; SC=CC=:CCY=EUR:ENV=:LC=en:LIM=:RS=m:TM=1444300641:TS=1444300641:TZ=:UCP=:VAT=:VER=; s_vi=[CS]v1|2AEE1E5585489697-60000103A002F05F[CE]; s_pers=%20s_fid%3D47DE135BD66B6E10-3A8420CADAC57A3E%7C1507459045330%3B%20gpv_p23%3Dskypeloginweb%252Faccount%252Flogin%7C1444302445338%3B%20s_nr%3D1444300645341-Repeat%7C1507372645341%3B; s_sess=%20s_ria%3Dflash%252019%257C%3B%20s_cc%3Dtrue%3B%20s_sq%3D%3B";
// String payload="{\"username\":\"username\",\"method\":\"UnifiedMVP2\", \"password\":\"password\", \"timezone_field\":\"+05|30\", \"js_time\":\"1444029538.658\", \"session_token\":\"448f4256096509ca35740235e7b78f3306156c97\", \"client_id\":\"578134\", \"redirect_uri\":\"https://web.skype.com\", \"pie\":\"7iw9lnVzLBaE3pIAFTD+Wn6rY17lkifj+9rXTt8LAFcMaex++atApZ6r404safgR8cxliXnLP3PF2Gqd9HKwjzA2NDIzNTI5M2I0YjliZTMxYWE1NjYzMWYwNjRmNzdh\", \"etm\":\"+oMsu5T+fvyJC89yhfDfhduvhoAx2RzS84mqD43PNz5sepudpPTo2KLGoKXEei7Ee9gpvgZj2W2H/Uc+O+f8oDkyZTJlZDAyOTg4NDExM2QxNzllZmU4NjkyOTFjMmU4\" }";
URL myurl = new URL(httpsURL);
HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Cookies",cookies);
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
// con.connect();
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
// output.writeBytes(payload);
output.close();
try{
con.setDoInput(true);
}
catch (IllegalStateException e){
System.out.print("\nIllegal State Exception \n"+e);
}
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
System.out.print( (char)c );
input.close();
System.out.println("Response Code :"+con.getResponseCode());
System.out.println("Response Message :"+ con.getResponseMessage());
Map<String, List<String>> headerFields = con.getHeaderFields();
Set<String> headerFieldsSet = headerFields.keySet();
Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
while (hearerFieldsIter.hasNext()) {
String headerFieldKey = hearerFieldsIter.next();
if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) {
List<String> headerFieldValue = headerFields.get(headerFieldKey);
int n= headerFieldValue.size();
System.out.println("Number of cookies="+n);
for (String headerValue : headerFieldValue) {
System.out.println("Cookie Found...");
String[] fields = headerValue.split(";\r*|;\n*|;\t*|;\f*");
//System.out.println("header Value="+headerValue);
String cookieValue = fields[0];
String expires = null;
String path = null;
String domain = null;
boolean secure = false;
// Parse each field
for (int j = 1; j < fields.length; j++) {
if ("secure".equalsIgnoreCase(fields[j])) {
secure = true;
}
else if (fields[j].indexOf('=') > 0) {
String[] f = fields[j].split("=");
if ("expires".equalsIgnoreCase(f[0])) {
expires = f[1];
}
else if ("domain".equalsIgnoreCase(f[0])) {
domain = f[1];
}
else if ("path".equalsIgnoreCase(f[0])) {
path = f[1];
}
}
}
System.out.println("cookieValue:" + cookieValue);
System.out.println("expires:" + expires);
System.out.println("path:" + path);
System.out.println("domain:" + domain);
System.out.println("secure:" + secure);
System.out.println("*****************************************");
}
}
}
System.out.println("Response Code :"+con.getResponseCode());
System.out.println("Response Message :"+ con.getResponseMessage());
}
}