Java.net.HttpURLConnection类是另外一种访问Http资源的方式。HttpURLConnection类可以完全取代HttpGet和HttpPost类。
下面是使用的一些步骤:
1、使用Java.net.URL封装HTTP资源的url,并使用openConnection方法获得HttpURLConnection对象,如下:
URL url = new URL("访问地址");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
2、设置方法httpURLConnection.setRequestMethod("POST");这个方法参数值必须大写
3、设置输入的其他权限,如果要下载HTTP资源或向服务器上传数据,需要使用如下代码:
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
设置缓存:httpURLConnection.setUseCaches(false);
4、设置HTTP请求头:
httpURLConnection.setRequestProperty("Charset","UTF-8");
5、输入和输出流:
InputStream is = httpURLConnection.getInputStream();
OutputStream os = httpURLConnection.getOutputStream();
直接看一个例子:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import mobile.android.widget.file.browser.FileBrowser;
import mobile.android.widget.file.browser.OnFileBrowserListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Main extends Activity implements OnFileBrowserListener
{
@Override
public void onDirItemClick(String path)
{
}
@Override
public void onFileItemClick(String filename)
{
String uploadUrl = "http://192.168.17.104:8080/upload/UploadServlet";
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
}
catch (Exception e)
{
setTitle(e.getMessage());
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FileBrowser fileBrowser = (FileBrowser) findViewById(R.id.filebrowser);
fileBrowser.setOnFileBrowserListener(this);
}
}
以上是个人对HttpURLConnection的理解,希望对大家有用