一、使用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");
相关推荐
设置HTTP请求使用的方法:conn.setRequestMethod("GET"); 设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 conn.setConnectTimeout(6*1000); 获取服务器的响应码并进行判断,正确的响应码一般为200...
NULL 博文链接:https://zjutsoft.iteye.com/blog/1313754
android使用Java原生httpUrlConnection进行get请求
HttpURLConnection的get,post和图片加载
使用HttpURLConnection发送短信
发送请求:通过HttpURLConnection类中的getOutputStream()方法获取输出流并写入请求体,从而发送POST请求。 处理响应:通过HttpURLConnection类中的getInputStream()方法获取输入流并读取响应体,从而处理服务器...
Java利用HttpURLConnection发送post请求上传文件
用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入...
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
HttpUrlConnection 用Post方式提交请求 ,我找的不容易,需要的时候很重要
使用HttpUrlConnection实现上传文件服务器端代码,引用commons-fileupload-1.2.1.jar、commons-io-1.4.jar
HttpURLConnection的(get请求、post请求)
里面详细的写了post和get和HttpPost和HttpGet请求
HttpURLConnection请求和HttpClient请求...................................................................
httpurlconnection 获取服务器数据并解析
这是一个关于httpurlconnection 的网络post get 图片下载的Demo
远程连接服务器HttpURLConnection,该工具有上传到远程服务器的方法,供参考远程连接服务器HttpURLConnection,该工具有上传到远程服务器的方法,供参考
HttpUrlConnection对象模拟POST带JSON字符串的AJAX请求失败! 我的目的是抓取响应结果字符串"301",但抓取的结果是一堆HTML代码,哪位大神看看哪有毛病?
使用HttpURLConnection实现断点续传
HttpURLConnectionTest_Get_Post 里面包含HttpURLConnection实现的Get和post方法