Android Series: GET, POST and Multipart POST 请求

如果你的请求要加附件你要加入apache-mime4j-0.6.jar and httpmime-4.0.1.jar 到你建的工程当中

try {
        HttpClient client = new DefaultHttpClient ( ) ;  
        String getURL = "http://www.google.com" ;
        HttpGet get = new HttpGet (getURL ) ;
        HttpResponse responseGet = client. execute (get ) ;  
        HttpEntity resEntityGet = responseGet. getEntity ( ) ;  
        if (resEntityGet != null ) {  
                    //do something with the response
                    Log. i ( "GET RESPONSE",EntityUtils. toString (resEntityGet ) ) ;
                }
} catch ( Exception e ) {
    e. printStackTrace ( ) ;
}
2.
try {
        HttpClient client = new DefaultHttpClient ( ) ;  
        String postURL = "http://somepostaddress.com" ;
        HttpPost post = new HttpPost (postURL ) ;
            List <NameValuePair > params = new ArrayList <NameValuePair > ( ) ;
            params. add ( new BasicNameValuePair ( "user", "kris" ) ) ;
            params. add ( new BasicNameValuePair ( "pass", "xyz" ) ) ;
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity (params,HTTP. UTF_8 ) ;
            post. setEntity (ent ) ;
            HttpResponse responsePOST = client. execute (post ) ;  
            HttpEntity resEntity = responsePOST. getEntity ( ) ;  
            if (resEntity != null ) {    
                Log. i ( "RESPONSE",EntityUtils. toString (resEntity ) ) ;
            }
    } catch ( Exception e ) {
        e. printStackTrace ( ) ;
    }
3.
File file = new File ( "path/to/your/file.txt" ) ;
try {
         HttpClient client = new DefaultHttpClient ( ) ;  
          String postURL = "http://someposturl.com" ;
         HttpPost post = new HttpPost (postURL ) ;
     FileBody bin = new FileBody (file ) ;
     MultipartEntity reqEntity = new MultipartEntity (HttpMultipartMode. BROWSER_COMPATIBLE ) ;  
     reqEntity. addPart ( "myFile", bin ) ;
     post. setEntity (reqEntity ) ;  
     HttpResponse response = client. execute (post ) ;  
     HttpEntity resEntity = response. getEntity ( ) ;  
      if (resEntity != null ) {    
               Log. i ( "RESPONSE",EntityUtils. toString (resEntity ) ) ;
          }
} catch ( Exception e ) {
    e. printStackTrace ( ) ;
}
下面是一个例子

public void executeMultipartPost() throws Exception {
try {
//InputStream is = this.getAssets().open(“main.xml”); //c:/reno.jpg taka prava pot za winse
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
“http://dev.jurancic.com/krpovej/kranj.php”);

//postRequest.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

//Log.d(“PARAMETRI PRED ŠTELANJEM”,httpClient.getParams()());
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); //tud nc boljs
//httpClient.getParams().setParameter(“http.socket.timeout”, new Integer(115));
httpClient.getParams().setParameter(“http.useragent”, “Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3″);//

httpClient.getParams().setParameter(“http.protocol.content-charset”, “UTF-8″); //veze
httpClient.getParams().setParameter(“http.protocol.reject-relative-redirect”, false);
//httpClient.getParams().setParameter(“http.protocol.max-redirects”, 2);//http.protocol.max-redirects
httpClient.getParams().setParameter(“http.protocol.expect-continue”, false);//dela
//httpClient.getParams().setParameter(“http.protocol.allow-circular-redirects”, true); //dovolimo redirekte na isto lokacijo
//httpClient.getParams().setParameter(“http.connection.stalecheck”, true); veze

//http.connection.stalecheck

//httpClient.getParams().setBooleanParameter(
// “http.protocol.expect-continue”, false); //tole tud ne pomaga

//Log.i(“parametri”,httpClient.getClass().toString());
//byte[] data = IOUtils.toByteArray(is);
//InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data), “uploadedFile”);

StringBody zadeva = new StringBody(“Mobi”);
StringBody vsebina = new StringBody(“Test”);
StringBody podrocje = new StringBody(“23″);
StringBody skupina = new StringBody(“6″);
//StringBody mail= new StringBody(“luka@car.com”);
//StringBody telefon = new StringBody(“123456789″);
StringBody poslji=new StringBody(“Pošlji”);

MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); //”—————————293582696224464″ ,Charset.forName(“UTF-8″)

multipartContent.addPart(“_ctl8:_ctl1:_ctl0:ddlSkupine”, skupina);
multipartContent.addPart(“_ctl8:_ctl1:_ctl0:ddlPodrocja”, podrocje);
//multipartContent.addPart(“_ctl8:_ctl1:_ctl0:txtEPosta”, mail);
//multipartContent.addPart(“_ctl8:_ctl1:_ctl0:txtTelefon”, telefon);
//multipartContent.addPart(“_ctl8:_ctl1:_ctl0:MultiUpload:File1″, isb);
multipartContent.addPart(“_ctl8:_ctl1:_ctl0:txtZadeva”, zadeva);
multipartContent.addPart(“_ctl8:_ctl1:_ctl0:txtVsebina”, vsebina);
multipartContent.addPart(“_ctl8:_ctl1:_ctl0:cmdPoslji”,poslji); //komanda??

postRequest.setEntity(multipartContent);
HttpResponse res = httpClient.execute(postRequest);

Log.d(“HTTPKODA”,res.getStatusLine().toString() );

HttpEntity resEntity = res.getEntity();
if (resEntity != null) {
Log.d(“ODGOVOR”,EntityUtils.toString(resEntity));//tole je odgovor
}
httpClient.getConnectionManager().shutdown(); //sprostimo resurse

//res.getEntity().getContent().close(); //ce tole zivo javi napako
} catch (IOException e) {
throw e;
}

}

我在参加ctf新生赛,这是一道web题目,叫做ez??upload 给了一个网站http://160.30.231.222:33360/ 源代码是<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Upload</title> <style> body { font-family: Arial, sans-serif; background-color: #f2f2f2; } .container { max-width: 400px; margin: 0 auto; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .form-group { margin-bottom: 20px; } .form-group label { display: block; font-weight: bold; margin-bottom: 5px; } .form-group input[type="file"] { padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #fff; } .form-group input[type="submit"] { padding: 10px 20px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer; } .form-group input[type="submit"]:hover { background-color: #45a049; } </style> </head> <body> <div class="container"> <h2>文件上传</h2> <form action="index.php" method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="file">选择文件</label> <input type="file" name="file" id="file" /> </div> <div class="form-group"> <input type="submit" value="上传" /> </div> </form> </div> </body> </html> <!-- class.php --> Hacker? 把网站后缀加上/class.php得到<?php error_reporting(0); highlight_file(__FILE__); class hacker{ public $cmd; public $a; public function __destruct(){ if('hahaha' === preg_replace('/;+/','hahaha',preg_replace('/[A-Za-z_\(\)]+/','',$this->cmd))){ eval($this->cmd.'hahaha!'); } else { echo 'nonono'; } } } if(isset($_POST['file'])) { if(preg_match('/^phar:\/\//i',$_POST['file'])) { die("nonono"); } file_get_contents($_POST['file']); } ?> 我该怎么做才能找到flag,一步一步详细来讲,我是纯新手注意我是纯新手!
10-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值