package com.musicplayer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import android.util.Log;
public class HttpGetProxy {
final private String LOCAL_IP_ADDRESS = "127.0.0.1";
final private int HTTP_PORT = 80;
private ServerSocket localServer = null;
private Socket localSocket = null;
private Socket remoteSocket = null;
private String remoteIPAddress;
private InputStream in_remoteSocket;
private OutputStream out_remoteSocket;
private InputStream in_localSocket;
private OutputStream out_localSocket;
private interface OnFinishListener {
void onFinishListener();
}
public HttpGetProxy(int localport) {
// --------建立代理中转服务器-----------//
try {
localServer = new ServerSocket(localport, 1,
InetAddress.getByName(LOCAL_IP_ADDRESS));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 结束时,清除所有资源
*/
private OnFinishListener finishListener =new OnFinishListener(){
@Override
public void onFinishListener() {
System.out.println("..........release all..........");
Log.e("---->","..........release all..........");
try {
in_localSocket.close();
out_remoteSocket.close();
in_remoteSocket.close();
out_localSocket.close();
localSocket.close();
remoteSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void startProxy(String remoteIpAddr) throws IOException {
remoteIPAddress = remoteIpAddr;
SocketAddress address = new InetSocketAddress(remoteIPAddress,HTTP_PORT);
// --------连接目标服务器---------//
remoteSocket = new Socket();
remoteSocket.connect(address);
System.out.println("..........remote Server connected..........");
Log.e("---->","..........remote Server connected..........");
in_remoteSocket = remoteSocket.getInputStream();
out_remoteSocket = remoteSocket.getOutputStream();
System.out.println("..........init remote Server I/O..........");
/**
* 接收本地request,并转发到远程服务器
*/
new Thread() {
public void run() {
int bytes_read;
byte[] local_request = new byte[5120];
try {
// 本地Socket
localSocket = localServer.accept();
System.out.println("..........localSocket connected..........");
Log.e("---->","..........localSocket connected..........");
in_localSocket = localSocket.getInputStream();
out_localSocket = localSocket.getOutputStream();
System.out.println("..........init local Socket I/O..........");
Log.e("---->","..........init local Socket I/O..........");
String buffer = "";
while ((bytes_read = in_localSocket.read(local_request)) != -1) {
String str = new String(local_request);
System.out.println("localSocket " + str);
Log.e("localSocket---->",str);
buffer = buffer + str;
if (buffer.contains("GET")
&& buffer.contains("\r\n\r\n")) {
//---把request中的本地ip改为远程ip---//
buffer = buffer.replace(LOCAL_IP_ADDRESS,remoteIPAddress);
System.out.println("已经替换IP");
out_remoteSocket.write(buffer.getBytes());
out_remoteSocket.flush();
continue;
} else{
out_remoteSocket.write(buffer.getBytes());
out_remoteSocket.flush();
}
}
System.out.println("..........local finish receive...........");
Log.e("---->","..........local finish receive..........");
finishListener.onFinishListener();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
/**
* 接收远程服务器reply,并转发到本地客户端
*/
new Thread() {
public void run() {
int bytes_read;
byte[] remote_reply = new byte[5120];
try {
System.out.println("..........remote start to receive...........");
Log.e("---->","..........remote start to receive..........");
while ((bytes_read = in_remoteSocket.read(remote_reply)) != -1) {
//System.out.println("remoteSocket " + remote_reply.length);
//System.out.println("remoteSocket " + new String(remote_reply));
out_localSocket.write(remote_reply, 0, bytes_read);
out_localSocket.flush();
}
System.out.println("..........remote finish receive...........");
Log.e("---->","..........remote finish receive..........");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android http server
共66个文件
class:39个
java:6个
prefs:4个
5星 · 超过95%的资源 需积分: 10 41 下载量 44 浏览量
2014-06-26
19:35:30
上传
评论
收藏 112KB ZIP 举报
温馨提示
转载 咪当我系欧巴 代码,原始链接在http://download.csdn.net/detail/hellogv/4047134
资源推荐
资源详情
资源评论
收起资源包目录
HttpGetProxy_player.zip (66个子文件)
HttpGetProxy_player
HttpGetProxy
musicplayer_demo
bin
classes.dex 13KB
res
drawable
icon.png 2KB
com
musicplayer
Player$1.class 963B
WebServer.class 3KB
R$layout.class 385B
CallBack.class 420B
test_musicplayer$ClickEvent.class 1KB
R$id.class 477B
R$drawable.class 391B
test_musicplayer$SeekBarChangeEvent.class 1KB
R$attr.class 331B
HttpConstants.class 1KB
Worker.class 8KB
R.class 510B
Player.class 4KB
test_musicplayer.class 4KB
R$string.class 418B
Player$2.class 1KB
MusicPlayer.apk 13KB
classes
classes.dex 13KB
com
musicplayer
Player$1.class 963B
HttpGetProxy$OnFinishListener.class 255B
HttpGetProxy$1.class 2KB
R$layout.class 385B
test_musicplayer$ClickEvent.class 1KB
R$id.class 414B
R$drawable.class 391B
test_musicplayer$SeekBarChangeEvent.class 1KB
R$attr.class 331B
R.class 510B
HttpGetProxy.class 4KB
Player.class 4KB
test_musicplayer.class 2KB
R$string.class 418B
Player$2.class 1KB
HttpGetProxy$2.class 3KB
HttpGetProxy$3.class 2KB
MusicPlayer.apk 13KB
resources.ap_ 5KB
resources.ap_ 5KB
res
drawable-ldpi
drawable-hdpi
drawable
icon.png 3KB
values
strings.xml 173B
drawable-mdpi
layout
main.xml 1KB
assets
default.properties 391B
gen
com
musicplayer
R.java 793B
.settings
org.eclipse.core.resources.prefs 170B
org.eclipse.jdt.core.prefs 629B
src
com
musicplayer
test_musicplayer.java 2KB
HttpGetProxy.java 5KB
Player.java 4KB
.project 847B
.classpath 288B
project.properties 391B
AndroidManifest.xml 756B
HttpGetProxy
bin
HttpGetProxy$OnFinishListener.class 223B
HttpGetProxy$1.class 1KB
HttpGetProxy.class 3KB
testHttpGetProxyForJ2SE.class 728B
HttpGetProxy$2.class 3KB
HttpGetProxy$3.class 1KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
src
HttpGetProxy.java 5KB
testHttpGetProxyForJ2SE.java 582B
.project 388B
.classpath 232B
共 66 条
- 1
资源评论
- wayslin2015-05-28测试通过,特来感谢
- lililove2016-07-13可以实现,但是我是需要处理get,post请求的方式
- fighter_water2015-10-11资源虽好,但不是我想要的那种webserver。
- autsluo2015-08-01好东西,果断下载,测试通过
haima1998
- 粉丝: 956
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功