Web应用程序有一个php脚本,可以将任务添加到故障单(每个故障单没有任务或多个任务),我想从我的类(ConnectionPost)调用此脚本,就像我使用浏览器一样。 这是ConnectionPost的代码:
import ...
public class ConnectionPost {
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
HttpClient client = new DefaultHttpClient();;
HttpPost httpPost;
HttpResponse reponce;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
BasicHttpContext myHttpContext = new BasicHttpContext();
BasicCookieStore myCookieStore = new BasicCookieStore();
HttpResponse loginResponce;
String cook;
public void makeConn(){
httpPost = new HttpPost("http://169.254.101.101:80/front/tickettask.form.php");
// adding task parameters to httpPost
nameValuePairs.add(new BasicNameValuePair("content", "tache ajouté post android"));
nameValuePairs.add(new BasicNameValuePair("tickets_id", "7"));
nameValuePairs.add(new BasicNameValuePair("taskcategories_id", "1"));
nameValuePairs.add(new BasicNameValuePair("state", "1"));
nameValuePairs.add(new BasicNameValuePair("is_private", "0"));
nameValuePairs.add(new BasicNameValuePair("actiontime", "60"));
nameValuePairs.add(new BasicNameValuePair("add", "ajouter"));
// define cookieStore
myHttpContext.setAttribute(ClientContext.COOKIE_STORE, myCookieStore);
//calling login() to log in
login();
try {
// setting headers
httpPost.setHeader("Referer", "http://169.254.101.101/front/ticket.form.php?id=7");
httpPost.setHeader("Origin","http://169.254.101.101");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Thread t = new Thread() {
@Override
public void run() {
try {
reponce = client.execute(httpPost,myHttpContext);
Log.d("makeConn excution de la req task","succés");
latch2.countDown();
} catch (ClientProtocolException e) {
Log.d("makeConn excution de la req",e.getMessage());
} catch (IOException e) {
Log.d("makeConn IOexception",e.getMessage());
}
}
};
t.start();
try {
latch2.await();
} catch (InterruptedException e) {
Log.d("latch login exception", e.getMessage());
}
//====================== display response
if(reponce != null){
StatusLine status = reponce.getStatusLine();
int codeStatus = status.getStatusCode();
if(codeStatus != 200){
Log.d("statusCode", codeStatus+"");
}
else{
HttpEntity corps = reponce.getEntity();
InputStream is;
try {
is = corps.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder s = new StringBuilder();
String r;
while((r = br.readLine()) != null){
s.append(r);
}
Log.d("Login succées",s.toString());
} catch (IllegalStateException e) {
Log.d("illegal makeConn excep", e.getMessage());
} catch (IOException e) {
Log.d("illegal makeConn IOexcep", e.getMessage());
}
}
}
else{
Log.d("reponse login", "null");
}
}
catch (UnsupportedEncodingException e) {
Log.d("makeConn conversion de la liste",e.getMessage());
}
}
public void login(){
final HttpPost loginRequest = new HttpPost("http://169.254.101.101:80/login.php");
List<NameValuePair> loginValuePairs = new ArrayList<NameValuePair>(2);
loginValuePairs.add(new BasicNameValuePair("noAuto", "1"));
loginValuePairs.add(new BasicNameValuePair("login_name", "glpi"));
loginValuePairs.add(new BasicNameValuePair("login_password", "glpi"));
try {
Thread t = new Thread() {
@Override
public void run() {
try {
loginRequest.setEntity(new UrlEncodedFromEntity()loginValuePairs);
loginRequest.setHeader("Referer","http://169.254.101.101")
loginResponce = client.execute(loginRequest,myHttpContext);
latch1.countDown();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
try {
latch1.await();
} catch (InterruptedException e) {
Log.d("latch login exception", e.getMessage());
}catch (ClientProtocolException e) {
Log.d("login protocol ecx", e.getMessage());
} catch (IOException e) {
Log.d("login IOExc", e.getMessage());
}
}
}
login()方法返回一个javascript文件:
06-01 13:03:27.518: D/Login succées(3801):
--------------------------------------------
NomNav = navigator.appName;
if (NomNav=='Konqueror') {
window.location='/index.php?error=1';
} else {
window.location='/index.php?error=1';
}
根据index.php,此页面将显示以下消息: “你必须接受COOKIES才能达到此申请”
然而,makeConn()方法返回一个重定向到(login.php),身份验证页面的javascript文件,就像我没有登录一样!