之前在自己公司做的手机OS上开发,最痛苦的就是我们的系统没有现成的HTTP协议,只有使用开源库的libCurl进行封装,在此过程中很好的熟悉了HTTP请求的一些细节,现在想想还是不错的一个经历,现在转到Android上了,对于Google来说,如果联网都处理不好的话,号称最好的互联网公司就太逊了吧。
HttpURLConnection conn=null;
URL url;
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/octet-stream, */*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","application/octet-stream");
conn.connect();
int code=conn.getResponseCode();
int len=conn.getContentLength();
if(conn==null)
{
return null;
}
InputStream is=null;
byte[] result=null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
is=conn.getInputStream();
while(true){
byte[] bytes = new byte[64];
int tmp=is.read(bytes);
if(tmp==-1)
{
break;
}
else
{
bos.write(bytes, 0, tmp);
}
}
result = bos.toByteArray();