package com.hjc.http;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class HttpMethodDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "http://localhost:8080/3-2-server//HttpServer";
String reslut = "";
try {
reslut = httpPost(path);
System.out.println(reslut);
reslut = httpGet(path);
System.out.println(reslut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* GET方法demo
* @param path
* @return
* @throws IOException
*
* */
public static String httpGet(String path) throws IOException {
URL url = new URL(path + "?param1=hjc1¶m2=hjc2");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sBuffer = new StringBuffer();
String line = "";
while( (line = reader.readLine() ) != null) {
sBuffer.append(line).append("\r\n");
}
reader.close();
return sBuffer.toString();
}
/*
*
* POST方法demo
* @param path
* @return
* @throws IOException
*/
public static String httpPost(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
writer.write("param1=hjc1¶m2=hjc2");
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sBuffer = new StringBuffer();
String line = "";
while( (line = reader.readLine() ) != null ) {
sBuffer.append(line).append("\r\n");
}
reader.close();
return sBuffer.toString();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
深度解析java游戏服务器开发.zip (39个子文件)
JAVA_GAME_CODE-main
RemoteSystemsTempFiles
.project 277B
3-2-server
.classpath 669B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 357B
.jsdtscope 491B
org.eclipse.wst.common.component 475B
org.eclipse.wst.common.project.facet.core.xml 296B
org.eclipse.wst.jsdt.ui.superType.container 49B
src
com
hjc
server
HttpServer.java 1KB
build
classes
com
hjc
server
HttpServer.class 2KB
WebContent
WEB-INF
web.xml 976B
META-INF
MANIFEST.MF 36B
.project 1007B
Socket-Tcp
.classpath 296B
.settings
org.eclipse.jdt.core.prefs 587B
src
com
hjc
server
SocketServer.java 1KB
SocketClientDemo.java 2KB
.project 369B
.gitignore 5B
3-2-client
.classpath 386B
.settings
org.eclipse.jdt.core.prefs 767B
src
com
hjc
http
HttpMethodDemo.java 2KB
.project 369B
.gitignore 6B
READEME.md 14B
Servers
.settings
org.eclipse.wst.server.core.prefs 109B
Tomcat v7.0 Server at localhost-config
catalina.policy 12KB
catalina.properties 6KB
context.xml 1KB
web.xml 159KB
server.xml 6KB
tomcat-users.xml 1KB
.project 200B
3-1
.classpath 386B
.settings
org.eclipse.jdt.core.prefs 767B
src
com
hjc
http
MainClass.java 1KB
.project 362B
.gitignore 6B
.gitignore 12B
共 39 条
- 1
资源评论
马coder
- 粉丝: 1198
- 资源: 6602
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功