import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HttpsURLConnection;
/*
*
*/
public class TestCreateContact {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// System.setProperty("http.proxyPort", "8080");
// System.setProperty("http.proxyHost", "10.99.60.242");
String cookie = login("k@1.1", "1234");
System.out.println(cookie);
URL url = new URL("https://uss.test.lenovomm.cn/contact/contact/portal/create/contact");
URLConnection con = url.openConnection();
con.addRequestProperty("Cookie", "GSESSIONID="+cookie);
con.setDoOutput(true);
con.setDoInput(true);
OutputStream out = con.getOutputStream();
out.write("NAME%C2%A7-1%C2%A9givenName=name&PHONE%C2%A7MOBILE%C2%B60=%E7%94%B5%E8%AF%9D&EMAIL%C2%A7HOME%C2%B60=%E9%82%AE%E7%AE%B1".getBytes());
out.close();
InputStream in = con.getInputStream();
Map<String, List<String>> headers = con.getHeaderFields();
for(Entry<String, List<String>> en:headers.entrySet()){
System.out.println(en.getKey());
for(String v:en.getValue())
System.out.println("\t"+v);
}
int allLen = con.getHeaderFieldInt("", -1);
if(allLen!=-1)
System.out.println(allLen);
System.out.println();
byte[] data=new byte[1024];
int n;
while((n=in.read(data))!=-1){
System.out.write(data, 0, n);
}
System.out.println();
in.close();
}
/**
* @param string
* @param string2
* @return
* @throws IOException
*/
private static String login(String name, String pwd) throws IOException {
URL url = new URL("https://uss.test.lenovomm.cn/wauthen/sublogin");
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
OutputStream out = con.getOutputStream();
out.write(("t=96749747&idtype=&lang=zh_CN&lenovoid.realm=cloud.lps.lenovo.com&lenovoid.ctx=aHR0cHM6Ly91c3MudGVzdC5sZW5vdm9tbS5jbi9jb250YWN0L3BvcnRhbC9pbmRleC5odG1s&lenovoid.lang=zh_CN&lenovoid.uinfo=username%2Cnickname%2Cpid&lenovoid.cb=https%3A%2F%2Fuss.test.lenovomm.cn%2Fcontact%2Fcontact%2Fportal%2FinitSession&lenovoid.vp=&c=&display=pcm&loginname="+URLEncoder.encode(name,"utf-8")+"&passwd="+URLEncoder.encode(pwd,"utf-8")).getBytes());
out.close();
InputStream in = con.getInputStream();
Map<String, List<String>> headers = con.getHeaderFields();
// for(Entry<String, List<String>> en:headers.entrySet()){
System.out.println(en.getKey());
for(String v:en.getValue())
System.out.println("\t"+v);
// }
// int allLen = con.getHeaderFieldInt("", -1);
// if(allLen!=-1)
// System.out.println(allLen);
// System.out.println();
ByteArrayOutputStream buff = new ByteArrayOutputStream(1024*5);
byte[] data=new byte[1024];
int n;
while((n=in.read(data))!=-1){
buff.write(data, 0, n);
}
// System.out.println();
in.close();
String content = new String(buff.toByteArray(), "utf-8");
// System.out.println(content);
int i = content.indexOf("url=");
String loginUrl = content.substring(i+"url=".length(), content.indexOf("\"", i));
// System.out.println(loginUrl);
// System.out.println();
// System.setProperty("http.proxyPort", "8080");
// System.setProperty("http.proxyHost", "127.0.0.1");
// url = new URL("http://127.0.0.1:8080/contact/contact/portal/initSession?lenovoid.action=uilogin&lenovoid.realm=cloud.lps.lenovo.com&lenovoid.wust=ZAgAAAAAAAGE9MTAwMDAxNjUwMzkmYj0xJmM9MSZkPTEwMzIxJmU9M0U4M0QyOTY1RDVDQzAxQjlDRjVDNDNGMjI0Q0ZEMjIxJmg9MTM0NTYzMjg0MjgzMyZpPTg2NDAwJnVzZXJuYW1lPWslNDAxLjEmcGlkPTEwMDAwMTEzMDNUsgFnljE1edDSwMblie0Y&lenovoid.ctx=aHR0cHM6Ly91c3MudGVzdC5sZW5vdm9tbS5jbi9jb250YWN0L3BvcnRhbC9pbmRleC5odG1s&lenovoid.lang=zh_CN&lenovoid.cb=https://uss.test.lenovomm.cn/contact/contact/portal/initSession&lenovoid.vp=&display=pcm");
url = new URL(loginUrl);
HttpsURLConnection.setFollowRedirects(false);
HttpsURLConnection http = (HttpsURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1");
http.setDoInput(true);
http.setDoOutput(false);
headers = http.getHeaderFields();
in = con.getInputStream();
for(Entry<String, List<String>> en:headers.entrySet()){
// System.out.println(en.getKey());
if("Set-Cookie".equals(en.getKey())){
for(String v:en.getValue()){
if(v.startsWith("GSESSIONID=")){
return v.substring("GSESSIONID=".length(), v.indexOf(";"));
}
// System.out.println("\t"+v);
}
}
}
// while((n=in.read(data))!=-1){
// System.out.write(data, 0, n);
// }
// System.out.println();
in.close();
http.disconnect();
return null;
}
}