Applet HttpURLConnect & HttpClient

来源:互联网 发布:淘宝上索尼z3c屏幕总成 编辑:程序博客网 时间:2024/06/05 01:17

/**
* HttpURLConnection 操作
* @param URL 请求的URL
* @param method 请求类型 GET/POST
* @return 获取到的结果 如果为null则表示出错
*
*/
public static String connectionHandle(final String URL,final String method,final String params){
Object obj=AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
StringBuffer contentBuffer = new StringBuffer();
HttpURLConnection conn=null;
int responseCode = -1;
URL url=null;
try {
url=new URL(URL);
conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);// IE代理进行下载
conn.setDoInput(true);
if (method.equals(“POST”)) {
conn.setDoOutput(true);// 是否输入参数
// 表单参数与get形式一样
byte[] bypes = params.toString().getBytes();
conn.getOutputStream().write(bypes);// 输入参数
}
conn.connect();
// 获得网页返回信息码
responseCode = conn.getResponseCode();
if (responseCode == -1) {
System.out.println(url.toString() + ” : connection is failure…”);
conn.disconnect();
return null;
}
if (responseCode >= 400) // 请求失败
{
System.out.println(“请求失败:get response code: ” + responseCode);
conn.disconnect();
return null;
}
InputStream inStr = conn.getInputStream();
InputStreamReader istreamReader = new InputStreamReader(inStr, “utf-8”);
BufferedReader buffStr = new BufferedReader(istreamReader);
String str = null;
while ((str = buffStr.readLine()) != null)
contentBuffer.append(str);
inStr.close();
} catch (IOException e) {
e.printStackTrace();
contentBuffer = null;
System.out.println(“error: ” + url.toString());
} finally {
conn.disconnect();
}
return contentBuffer.toString();
}
});
return (obj!=null)?(String)obj:null;
}

/**
* httpClient POST 请求
*
* @param host 主机名
* @param port 端口
* @param uri 请求的方法 like:”/VTFileWeb/test.do?method=getPro” or “/?”
* @param nvps 请求的参数 request.getParameter();
*
* @return 请求结果
*/
public static String httpClientConnectHandle(final String host,final int port,final String uri,final NameValuePair[] nvps){
Object obj=AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
String protocol=”http”;
HttpClient client=new HttpClient();
client.getHostConfiguration().setHost(host, port, protocol);
PostMethod postMethod=new PostMethod(uri);
postMethod.setRequestHeader(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt”);// IE代理进行下载
// NameValuePair nvp=new NameValuePair(“param”,param);
if (nvps!=null) {
postMethod.setRequestBody(nvps);
}
try {
client.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String result=null;
InputStream in=null;
InputStreamReader istreamReader =null;
try {
in=postMethod.getResponseBodyAsStream();
istreamReader = new InputStreamReader(in, “utf-8”);
StringBuffer sb=new StringBuffer();
int c;
while ((c=istreamReader.read())!=-1) {
sb.append((char)c);
}
result=sb.toString();
System.out.println(“结果:”+result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
istreamReader.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
});
return (String)obj;

0 0