一、使用HttpURLConnection向服务器发送get请求

1、向服务器发送get请求

@Test
publicvoid sendSms() throws Exception{
String message="货已发到";
message=URLEncoder.encode(message, "UTF-8");
System.out.println(message);
String path ="http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+message;
URL url =new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = StreamTool.readInputStream(inStream);
String result=new String(data, "UTF-8");
System.out.println(result);
}

2、从服务器读取数据

String message=request.getParameter("message");

二、使用HttpURLConnection向服务器发送post请求

1、向服务器发送post请求

@Test
publicvoid addByUrl() throws Exception{
String encoding="UTF-8";
String params="[{\"addTime\":\"2011-09-19 14:23:02\"[],\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
String path ="http://localhost:8083/xxxx/xxx/sim!add.do";
byte[] data = params.getBytes(encoding);
URL url =new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
conn.setRequestProperty("Content-Type", "application/x-javascript; charset="+ encoding);
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5*1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode()); //响应代码 200表示成功
if(conn.getResponseCode()==200){
InputStream inStream = conn.getInputStream();
String result=new String(StreamTool.readInputStream(inStream), "UTF-8");
}
}

2、从服务器读取数据

//获取post请求过来的数据
byte[] data=StreamTool.readInputStream(request.getInputStream());
//[{\"addTime\":\"2011-09-19 14:23:02\"[],\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]
String json=new String(data, "UTF-8");

使用HttpURLConnection向服务器发送post和get请求的更多相关文章

  1. 使用HttpURLConnection向服务器发送post和get请求(转)

    一.使用HttpURLConnection向服务器发送get请求 1.向服务器发送get请求 @Test publicvoid sendSms() throws Exception{ String m ...

  2. 【03】AJAX 向服务器发送请求

    AJAX 向服务器发送请求   创建 XMLHttpRequest 对象后,就可以向服务器发送请求了. XMLHttpRequest 对象的 open() 方法和 send() 方法用来向服务器发送请 ...

  3. postman(二):使用postman发送get or post请求

    总结一下如何使用postman发送get或post请求 请求 一.GET请求 通常用于请求服务器发送某个资源,请求的数据会附在URL之后,以?分割URL和传输数据,多个参数用&连接 1.请求方 ...

  4. Android使用HttpUrlConnection请求服务器发送数据详解

    HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...

  5. Android - 向服务器发送数据(GET).

    在此,使用HTTP协议,通过GET请求,向服务器发送请求,这种方式适合于数据量小,数据安全性要求不高的情况下. 一,服务器端,使用Servlet. 在服务器端,定义一个HttpServlet的子类,以 ...

  6. Http学习之使用HttpURLConnection发送post和get请求(3)

    使用HttpURLConnection发送post和get请求 但我们常常会碰到这样一种情况: 通过HttpURLConnection来模拟模拟用户登录Web服务器,服务器使用cookie进行用户认证 ...

  7. Http学习之使用HttpURLConnection发送post和get请求(2)

    接上节Http学习之使用HttpURLConnection发送post和get请求 本节深入学习post请求. 上 节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在ge ...

  8. HttpURLConnection发送GET、POST请求

    HttpURLConnection发送GET.POST请求 /** * GET请求 * * @param requestUrl 请求地址 * @return */ public String get( ...

  9. Android 给服务器发送网络请求

    今天听得有点蒙,因为服务器的问题,这边建立服务器的话,学长用的是Idea建立的Spring之类的方法去搞服务器. 然后就是用Android去给这个服务器发送请求,大致效果还是懂的,就是像网站发送请求, ...

随机推荐

  1. FWFT FIFO读操作注意

    FWFT:First Word Fall Through的缩写,好像是Xilinx的说法,Altera对应的概念是Show-ahead synchronous(SASO).即数据在rdreq有效之前就 ...

  2. Apache 配置多端口 多虚拟主机 局域网访问

    \wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf 修改如下 NameVirtualHost *:80          Documen ...

  3. java Spring 生命周期

    1.初始化回调 <bean name="userService" class="com.sun.service.UserService" init-met ...

  4. 再跟SQL谈一谈--基础篇

    1.简介 2.DDL & DML 3.SELECT ①DISTINCT ②WHERE ③AND & OR ④ORDER BY 4.INSERT 5.UPDATE 6.DELETE 1. ...

  5. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...

  6. php模板引擎

    http://baike.baidu.com/link?url=HmXfdJBv3zpCdnZPeaSmZmqDBHlyTBnz9Rmb5it-jf1_NLHfaku6_i8ssUYbnaTQEBD4 ...

  7. Javascript中数组方法汇总

    Array.prototype中定义了很多操作数组的方法,下面介绍ECMAScript3中的一些方法: 1.Array.join()方法 该方法将数组中的元素都转化为字符串并按照指定符号连接到一起,返 ...

  8. [jquery] jQuery点滴[持续更新]

    001.查看jquery的版本. $(function(){ console.log($()); //jquery console.log($().jquery); }); 002.(new Func ...

  9. vs的watch使用

    VC调试器高级应用----WATCH窗口篇 一.格式化数据和表达式赋值语句. 常用变量格式化符(表达式的值后跟逗号,接格式化符,如"(int)0xFFFF,d"):d  :有符号的 ...

  10. 原型模式(Prototype Pattern)

    原型模型:用于创建重复对象,同时保证性能. 这种模式实现一个原型接口,用于创建对象的克隆,当直接创建对象的代价比较大,则可以采用这种模式.例如:一个对象需要高代价的数据库操作之后被创建,这时可以缓存该 ...