📄 appletrequest.java
字号:
package com.oyc.mapxtreme.applet.util;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
* 负责Applet向服务器发送请求: doGet(),doPost()
* @author 三峡大学理学院 欧阳超
*
*/
public class AppletRequest {
/**
* 发送get请求,并返回响应流的的字节数据
* @param url 请求的url
*
* @return 响应流中读取的字节数组
* @throws IOException
*/
public byte[] sendGetRequest(String url) throws IOException {
DataInputStream input = null;
byte[] b = new byte[100000];
try {
URL server = new URL(url);
URLConnection con = server.openConnection();
input = new DataInputStream(con.getInputStream());
//读出数据
try {
input.readFully(b); //EOFException
} catch (Exception e) {
}
} catch (IOException e) {
System.out.println("发送get请求出错...");
throw e;
} finally {
input.close();
}
return b;
}
/**
* 发送post请求,并返回响应流的的字节数据
* @param url 请求的url
* @param parameter 传入的参数,形如name=ouyang&sex=1.....
*
* @return 响应流中读取的字节数组
* @throws IOException
*/
public byte[] sendPostRequest(String url, String parameter) throws IOException {
//建立HTTP连接
HttpURLConnection httpConn = null;
PrintWriter out = null;
try {
URL server = new URL(url);
httpConn = (HttpURLConnection) server.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
//发送参数
out = new PrintWriter(httpConn.getOutputStream());
out.print(parameter);
} catch (IOException e) {
System.out.println("发送post请求出错...");
throw e;
} finally {
try {
if(out != null){
out.flush();
out.close();
}
} catch (Exception e) {
}
}
//读取响应流中的数据
DataInputStream input = null;
byte[] b = new byte[100000];
try {
input = new DataInputStream(httpConn.getInputStream());
try {
input.readFully(b); //EOFException
} catch (Exception e) {
}
} catch (IOException e) {
System.out.println("发送post请求出错...");
throw e;
} finally {
input.close();
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -