Java 如何使用HttpURLConnection获取Skype的身份验证令牌
我正在尝试使用JAVA中的HttpURLConnection类登录Skype中的用户。我的目标是为我的用户获取身份验证令牌(每24小时过期一次)。我正在跟踪每个HTTP调用中发送的请求cookie,并在JAVA程序中发送相同的cookie集。但当收到响应时,当我通过浏览器和HttpURLConnection类登录时,HTTP调用接收到的cookie会有所不同 有人能帮忙吗Java 如何使用HttpURLConnection获取Skype的身份验证令牌,java,cookies,oauth,httpurlconnection,skype,Java,Cookies,Oauth,Httpurlconnection,Skype,我正在尝试使用JAVA中的HttpURLConnection类登录Skype中的用户。我的目标是为我的用户获取身份验证令牌(每24小时过期一次)。我正在跟踪每个HTTP调用中发送的请求cookie,并在JAVA程序中发送相同的cookie集。但当收到响应时,当我通过浏览器和HttpURLConnection类登录时,HTTP调用接收到的cookie会有所不同 有人能帮忙吗 /** * Created by shreyas on 23/09/15. */ import java.io.Dat
/**
* 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());
}
}
/**
*由什里亚斯于2015年9月23日创作。
*/
导入java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.net.urlcoder;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
公共类HttpsURLConnection{
公共静态void main(字符串[]args)引发异常{
字符串httpsURL=”https://login.skype.com/login?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com&intsrc=client-_-webapp-uu-生产-uu-登录和消息=注销和lc=16393”;
String query=“username=”+URLEncoder.encode(“用户名”,“UTF-8”);
查询+=“&”;
查询+=“password=“+urlcoder.encode”(“密码”,“UTF-8”);
字符串cookies="目前,SWACC=1;SWACC=TM=1442036652;跟踪=144300567474353;跟踪=144300565674743;跟踪=1443006=1442036652;跟踪=144300566=14430056567;跟踪=144300567 7 7 7=7 7=ID=ID=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=7=1;Swacwawac=8;Swac抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄抄c=14=8=14=14=14=14=14=14=14=14=14=14=1442036=14=8=14=14=14=14=14=14=1442036=8=14=14=14=14=1442036=6=7=6=6=14 PE会话令牌=ed386369f29ac162d028d11fe4d6f365b462ba1c;SC=CC=:CCY=EUR:ENV=:LC=en:LIM=:RS=m:TM=1444300641:TS=1444300641:TZ=:UCP=:VAT=:VER=;s|U vi=[CS]v1 | 2EE5585489697-60000103A002F05F[CE];s_pers=%20s_fid%3D47DE135BD66B6E10-3A8420CADAC57A3E%7C150745904530%3B%20gpv_p23%3Dskypeloginweb%252Faccount%252Flogin%7C1444302445338%3B%20s_nr%3D1444300645341重复%7C1507372645341%3B;s_sess=%20s_ria%3Dflash%3Dflash%252019%257C%3B%20s_cc%3Dtrue%3B%3B%;
//字符串有效载荷=“{\'username\”:“username\”,“method\”:“UnifiedMVP2\”,“password\”:“password\”,“timezone\u field\”:“+05\12430\”,“js\u time\”:“1444029538.658\,“session\u token\”:“448F4256096509CA35740235E7B78F33066C97\”,“client\u id\”:“578134\,“redirect\u uri\”https://web.skype.com\“,”派“:\“7iw9lnVzLBaE3pIAFTD+Wn6rY17lkifj+9rXTt8LAFcMaex++ATAPZ6R404SAFGR8CXLIXNLP3PF2GQD9HKWJZZ2D2YJLZTMXYWE1NJYZMWYWNZNZDH\”,“etm\”:“+oMsu5T+FVYJC89YHFDFHDUVHOAX2RZ84MQD43PNZ5SEP9GPVGZJ2W2H/Uc+O+F8ODZTAYWZDZDZDZYW2NZYNZKYNZM2NZMKYFZMZMZMZMZM4FZMKYFZMZM4FZM4FZMZMZMZM4FZMZM;
URL myurl=新URL(httpsURL);
HttpURLConnection con=(HttpURLConnection)myurl.openConnection();
con.setRequestMethod(“POST”);
con.setRequestProperty(“Cookies”,Cookies);
con.setRequestProperty(“内容长度”,String.valueOf(query.length());
con.setRequestProperty(“内容类型”、“应用程序/x-www-表单urlencoded”);
con.setRequestProperty(“用户代理”、“Mozilla/4.0(兼容;MSIE 5.0;Windows98;DigExt)”);
//con.connect();
con.设置输出(真);
DataOutputStream输出=新的DataOutputStream(con.getOutputStream());
output.writeBytes(查询);
//输出写字节(有效载荷);
output.close();
试一试{
con.setDoInput(真);
}
捕获(非法状态){
System.out.print(“\nIllegal State Exception\n”+e);
}
DataInputStream输入=新的DataInputStream(con.getInputStream());
for(int c=input.read();c!=-1;c=input.read())
系统输出打印((字符)c);
input.close();
System.out.println(“响应代码:+con.getResponseCode());
System.out.println(“响应消息:+con.getResponseMessage());
Map headerFields=con.getHeaderFields();
Set headerfieldset=headerFields.keySet();
迭代器HeaderFieldSiter=HeaderFieldSet.Iterator();
while(hearFieldSiter.hasNext()){
字符串headerFieldKey=hearerFieldsIter.next();
如果(“设置Cookie.equalsIgnoreCase(headerFieldKey)){
List headerFieldValue=headerFields.get(headerFieldKey);
int n=headerFieldValue.size();
System.out.println(“cookies的数量=”+n);
for(字符串headerValue:headerFieldValue){
System.out.println(“找到Cookie…”);
字符串[]字段=headerValue.split(;\r*|;\n*|;\t*|;\f*);
//System.out.println(“标题值=“+headerValue”);
字符串cookieValue=字段[0];
字符串expires=null;
字符串路径=null;
字符串域=null;
布尔安全=假;
//分析每个字段
对于(int j=1;j0){
字符串[]f=字段[j]。拆分(“”);
如果(“过期”。equalsIgnoreCase(f[0])){
expires=f[1];
}
else if(“domain”.equalsIgnoreCase(f[0])){
域=f[1];
}
else if(“path”.equalsIgnoreCase(f[0])){
路径=f[1];
}
}
}
System.out.println(“cookieValue:+cookieValue”);
System.out.println(“expires:+expires”);
System.out.println(“路径:+path”);
系统退出。