Skip to content

Instantly share code, notes, and snippets.

@jen6
Created July 13, 2015 15:54
Show Gist options
  • Save jen6/d6bfdd378a65a939a5bd to your computer and use it in GitHub Desktop.
Save jen6/d6bfdd378a65a939a5bd to your computer and use it in GitHub Desktop.
class LoginSession {
static String Session = "";
static public String get_session() {
if (Session == ""){
HttpClient http = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
String url = "http://makeall.ml:8989/login";
try{
HttpContext context = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> nameValuePairs =
new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Id", "jen6"));
nameValuePairs.add(new BasicNameValuePair("Pw", "abcd")); //post 파라미터 셋팅
UrlEncodedFormEntity entityRequest =
new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(entityRequest);
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGet = new HttpGet("http://makeall.ml:8989/");
HttpResponse response = http.execute(httpGet, context);
CookieStore store = ((DefaultHttpClient)http).getCookieStore();
List<Cookie> cookies = store.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
Session = c.getValue();
}
}
}catch(Exception e){e.printStackTrace();}
}
return Session;
}
//글쓰기용
public class BusPostSetter {
public HttpClient http = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
// 쿠키스토어랑 httpclient를 퍼블릭으로 선언
// 프리레퍼런스 사용할 객체 선언
public BusPostSetter() {
}
public BusPostSetter(String Title, String Content, String Want) {
try {
HttpContext context = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://makeall.ml:8989/board/bus");
httpPost.addHeader("my_session", LoginSession.get_session());
ArrayList<NameValuePair> nameValuePairs =
new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Title", Title));
nameValuePairs.add(new BasicNameValuePair("Content", Content)); //post 파라미터 셋팅
nameValuePairs.add(new BasicNameValuePair("Want", Want));
UrlEncodedFormEntity entityRequest =
new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(entityRequest);
HttpResponse responsePost = http.execute(httpPost);
// 리턴값
Log.d("msg", "login");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment