This discussion is archived
5 Replies Latest reply: Aug 24, 2007 4:27 AM by 807597 RSS

Help me with one problem HttpClient

807597 Newbie
Currently Being Moderated
I have one program use HttpClient for login and update profile acount .
I must create 2 PostMothod .
Method 1 : Login . succesfull
Method 2 : Update . status code return 302 ( Logout program >> update not succesfull )

if i use one PostMethod for Login Webpage and After use more MethodGet >>>> Successfull and haven't problem .

Somebody have done this problem or known, please help me !


Code of program :

////////////////////////////////////// CLASS EDIT PROFILE //////////////////////

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.apache.commons.httpclient.ConnectTimeoutExcept ion;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.SimpleHttpConnection Manager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class EditProfile {
HttpClient client_ = null ;
static final String USER_GENT = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)";
static final String HOST = "http://www.gso-media.com/";
static final String REF_ADV = "http://www.gso-media.com/home/user_inbox.asp";
static final String COOKIE = "Set-Cookie" ;
public String sesid_ = null ;
private String contentData = "" ;

public static void main(String[] args) {
EditProfile editP = new EditProfile() ;
editP.post() ;
// City Number is 78 or 81
editP.Edit("78") ;
}


// Login Acount
public void post() {
HttpClient client = getHTTPClient();
client.getParams().setParameter("User-Agent", USER_GENT);
client.getParams().setParameter("Host", "gso-media.com");
client.getParams().setParameter("Accept-Encoding","gzip, deflate") ;
client.getParams().setParameter("Connection","Keep -Alive") ;
BufferedReader br = null;
PostMethod method = new PostMethod("http://www.gso-media.com/home/checkuser.asp");

method.setRequestHeader("Referer", "http://www.gso-media.com/home/");
method.addParameter("username", "dunglb90") ;
method.addParameter("pass", "098765") ;
method.addParameter("action", "login") ;
method.addParameter("B3","Đăng nhập") ;
//method.addParameter("repage","user_inbox.asp") ;
try {
int returnCode = client.executeMethod(method);
System.out.println("code ===" + returnCode);

if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.err.println("The Post method is not implemented by this URI");
method.getResponseBodyAsString();
} else if(HttpStatus.SC_MOVED_TEMPORARILY == returnCode|| HttpStatus.SC_MOVED_TEMPORARILY == returnCode
|| HttpStatus.SC_SEE_OTHER == returnCode|| HttpStatus.SC_TEMPORARY_REDIRECT == returnCode) {
String redirectLocation = "";
Header locationHeader = method.getResponseHeader("location");
System.out.println("redirect :"+redirectLocation);
if(locationHeader != null) {
redirectLocation = locationHeader.getValue();

if (redirectLocation.indexOf("http") == -1) redirectLocation = HOST + redirectLocation;
if(method != null) method.releaseConnection();
System.out.println("redirect to another page " + redirectLocation);
get(redirectLocation,null);
}
}else {
get("http://www.gso-media.com/home/user_edit.asp",null) ;
}
} catch (Exception e) {
System.err.println(e);
} finally {
method.releaseConnection();
if(br != null) {
try {
br.close() ;
} catch (Exception e) {
}
}
}
SearchLink search = new SearchLink() ;
sesid_ = search.getSessionId(contentData) ;


}

public HttpClient getHTTPClient() {
if(client_ == null) client_ = new HttpClient(new SimpleHttpConnectionManager()) ;
client_.getParams().setParameter("http.protocol.ex pect-continue", true);
return client_ ;
}

public boolean get(String url,String referer) {
GetMethod method = null;
OutputStream out = null;
if(client_ == null) client_ = getHTTPClient() ;

try{
method = new GetMethod(url);
method.setRequestHeader("User-Agent", USER_GENT);
if(referer == null) method.setRequestHeader("Referer", "");
else method.setRequestHeader("Referer", referer);
int status = client_.executeMethod(method);
if(status == 200) {
contentData = getContentSite(method.getResponseBodyAsStream()) ;
return true ;
} else if(HttpStatus.SC_MOVED_TEMPORARILY == status || HttpStatus.SC_MOVED_TEMPORARILY == status
||HttpStatus.SC_SEE_OTHER == status|| HttpStatus.SC_TEMPORARY_REDIRECT == status) {
if(out != null) {
out.flush() ;
out.close() ;
}
String redirectLocation = "";
Header locationHeader = method.getResponseHeader("location");
if(locationHeader != null) {
redirectLocation = locationHeader.getValue();
if (redirectLocation.indexOf("http") == -1) redirectLocation = HOST + redirectLocation;
if(method != null) method.releaseConnection();
System.out.println("redirect to another page " + redirectLocation);
return get(redirectLocation,null);
}
} else System.err.println("fail to get due to " + status);
}catch(ConnectTimeoutException timeout){
System.err.println("connection time out==> get new connection");
}catch (Exception e) {
e.printStackTrace() ;
} finally{
if(out != null) {
try {
out.flush() ;
out.close() ;
} catch (IOException e) {
e.printStackTrace();
}

}
if(method != null) method.releaseConnection() ;
}


return false ;
}

public void copy(InputStream in,OutputStream out) throws Exception {
int counter = 0;
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
counter++;
if (counter > 100) {
if(out != null) out.flush() ;
counter = 0;
}
}
}
}
}

public String getContentSite(InputStream in) throws Exception {
String content = "" ;
BufferedReader br = new BufferedReader(new InputStreamReader(in)) ;
String readLine;
while(((readLine = br.readLine()) != null)) {
content = content +"\n" + readLine ;
}
return content ;
}

// Update Profile
public void Edit(String CityID) {
PostMethod postEdit = new PostMethod("http://gso-media.com/home/user_edit.asp?action=update") ;
postEdit.setRequestHeader("Referer", "http://gso-media.com/home/user_edit.asp?"+sesid_) ;
postEdit.addParameter("Adress", "LB") ;
postEdit.addParameter("CityID", CityID) ;
postEdit.addParameter("CityName","HCM") ;
postEdit.addParameter("ComputerLevelID", "3") ;
postEdit.addParameter("CountryID", "201") ;
postEdit.addParameter("EducationID", "3") ;
postEdit.addParameter("JobID", "1") ;
postEdit.addParameter("Language", "1") ;
postEdit.addParameter("ProfessionID", "1") ;
postEdit.addParameter("TelephoneNumber", "") ;

try {
int st = client_.executeMethod(postEdit) ;
System.out.println("code post edit :"+st);

} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


///////////////////////////CLASS SEARCH/////////////////////////////////

package process;

import java.util.ArrayList;
import java.util.List;

public class SearchLink {
static final String DataTD = "td class=DataTD" ;
static final String ContentTD = "td class='ContentTD'" ;
static final String ClickLink = "B&#7841;n h�y click <a href=" ;
static final String MONEY = "S&#7889; d&#432; trong t�i kho&#7843;n" ;
static final String ParamLinkRun = "advconfirm_header.asp?" ;
static final String ParamLinkPDT = "user_inbox_vote.asp?" ;
static final String SesstionId = "sessionid" ;
private int index = 0 ;
private String session_ = "" ;

public List<String> search(String content,String value) {
String gmValue = null ;
if(value != null) gmValue = "<td class='DataTD'>"+value+"</td>" ;
// check inbox have mail
index = content.indexOf(DataTD,0) ;
if(index == -1) return null ;
content = content.substring(index,content.indexOf("class=hea derTD", index)) ;
//System.err.println(content);
//=========== Search link with value input ====================
List<String> links = new ArrayList<String>() ;
int indLink = 0 ;
int count = 0 ;
if(value == null) {
for( ; ; ) {
indLink = content.indexOf(ContentTD,count) ;
if(indLink == -1) break ;
int start = content.indexOf("'",indLink+ContentTD.length()) ;
int end = content.indexOf("'", start+1) ;
links.add(content.substring(start+1, end)) ;
count = end ;
}
} else {
for( ; ; ) {
indLink = content.indexOf(ContentTD,count) + ContentTD.length() ;
System.out.println("indLink2 ===="+indLink);
if(indLink == -1) break ;
int indLinkNext = content.indexOf(ContentTD,indLink) ;
String subContent = content.substring(indLink,indLinkNext) ;
if(subContent.indexOf(gmValue) != -1) {
int start = subContent.indexOf("'",indLink) ;
int end = subContent.indexOf("'", start) ;
links.add(subContent.substring(start, end)) ;
count = end ;
}

}
}

return links ;
}

public String getLinkAdv(String content) {
int subInd = content.indexOf(ClickLink)+ClickLink.length()+1 ;
String link = content.substring(subInd,content.indexOf("\"",subI nd)) ;
if(link != null || link.equals("")) {
//session_ = getSessionId(content) ;
return link ;
}
return null ;
}

public List<String> getLinkRun(String content) {
List<String> run = new ArrayList<String>() ;
int ind = content.indexOf(ParamLinkRun)+ParamLinkRun.length( );
String link = content.substring(ind,content.indexOf("\"",ind)) ;
run.add("http://www.gso-media.com/home/advconfirm.asp?"+link) ;
run.add("http://www.gso-media.com/home/advconfirm_header.asp?"+link) ;
return run ;
}

public List<String> getLinkPDT(String content) {
if(content == null) {
System.out.println("contend null ");
return null ;
}
index = content.indexOf(DataTD,0) ;
if(index == -1) return null ;
content = content.substring(index,content.indexOf("class=hea derTD", index)) ;
//System.err.println(content);
//=========== Search link with value input ====================
List<String> links = new ArrayList<String>() ;
int indLink = 0 ;
int count = 0 ;

for( ; ; ) {
indLink = content.indexOf(ContentTD,count) ;
if(indLink == -1) break ;
int start = content.indexOf(ParamLinkPDT,indLink)+ParamLinkPDT .length() ;
int end = content.indexOf("'", start+1) ;
String lk = "http://gso-media.com/home/user_inbox_vote.asp?"+content.substring(start+1, end) ;
System.out.println("Link chuan day : "+lk);
links.add("http://gso-media.com/home/user_inbox_vote.asp?"+content.substring(start+1, end)) ;
count = end ;
}
return links ;

}

public String getSessionId(String content) {
int indSession = content.indexOf(SesstionId) ;
try{
session_ = content.substring(indSession, content.indexOf("\"", indSession)) ;
}catch (StringIndexOutOfBoundsException e) {
return "abc" ;
}

return session_ ;
}

public String money(String content) {
int id = content.indexOf(MONEY) ;
int next = content.indexOf("align=",id) +14 ;

return content.substring(next,content.indexOf("<", next)) ;
}

}