1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public String postURL(URL Url,String sParamsToPost)
{
StringBuilder sb = new StringBuilder();
//recup du saut de ligne
String LineSep = null;
try
{
LineSep = System.getProperty("line.separator");
// LineSep = "<br />";
}
catch (Exception e)
{
LineSep = "\n";
}
try
{
HttpURLConnection UrlConn = (HttpURLConnection) Url.openConnection();
// UrlConn.setAllowUserInteraction(false);
UrlConn.setDoOutput(true);
UrlConn.addRequestProperty("Cache-Control", "max-age=0");
System.out.println(sParamsToPost);
if (!sParamsToPost.equals("")){
UrlConn.setRequestMethod("POST");
//poster les params
PrintWriter paramWriter = new PrintWriter(UrlConn.getOutputStream());
paramWriter.print(sParamsToPost);
//fermeture du post
paramWriter.flush();
paramWriter.close();
}else{
UrlConn.setRequestMethod("GET");
}
UrlConn.connect();
//lire les header de la reponse
Map<String, List<String>> map= UrlConn.getHeaderFields();
System.out.println("lecture MAP");
System.out.println(map.size());
Set<String> set = map.keySet();
for(String key : set){
System.out.println(key+" : "+map.get(key));
}
//Lire la reponse
InputStream response = UrlConn.getInputStream();
BufferedReader bufReader = new BufferedReader(new InputStreamReader(response));
String sLine;
while ((sLine = bufReader.readLine()) != null)
{
sb.append(sLine);
sb.append(LineSep);
}
//deconnection
UrlConn.disconnect();
}
catch(ConnectException ctx)
{
// Log.fatal(this, "Connection lost : server may be down");
ctx.printStackTrace();
}
catch (Exception e)
{
// Log.error(this,"postURL : "+e.getMessage());
e.printStackTrace();
}
//Log.debug(this, "retour url="+o_oSb.toString());
return sb.toString();
} |
Partager