JAVA中使用HttpURLConnection发送XML参数乱码问题
2017-03-08 10:20
706 查看
POST方法
调用
如果xml里面含有中文。需要在传送之前进行URLEncoder.encode(param,”utf-8”) 进行编码。
public static String post(String content,String httpUrl){ String result = ""; HttpURLConnection httpURLConnection = null; ByteArrayOutputStream outputStream = null; InputStream in = null; try{ URL url = new URL(httpUrl); httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setConnectTimeout(3 * 1000); httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); httpURLConnection.getOutputStream().write(content.getBytes()); httpURLConnection.getOutputStream().flush(); outputStream=new ByteArrayOutputStream(); in = httpURLConnection.getInputStream(); int len =0; byte[] bt=new byte[1024]; while((len=in.read(bt))!=-1){ outputStream.write(bt, 0, len); } outputStream.flush(); byte[] data=outputStream.toByteArray(); result=new String(data,"utf-8"); } catch (MalformedURLException e) { result = "error"; } catch (ProtocolException e) { result = "error"; } catch (IOException e) { e.printStackTrace(); result = "error"; }finally{ if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); result = "error"; } } if(outputStream!=null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); result = "error"; } } } return result; } }
调用
public static void main(String[] args){ String httpUrl = "localhost:8080/***/***" String logistics_interface = "<request>" +"<receiverProv>230000"//省 +"</receiverProv>" +"<receiverCity>231000"//市 +"</receiverCity>" +"<receiverCounty>231084"//区县 +"</receiverCounty>" +"<receiverAddress>此处是中文需要进行编码"//地址 +"</receiverAddress>" +"<receiverLongitude>46.123"//经纬度 +"</receiverLongitude>" +"<receiverLatitude>46.123" +"</receiverLatitude>" +"<startPage>1"//页码数 +"</startPage>" +"<rowNum>10"//每页条目数 +"</rowNum>" +"</request>"; String data_digest = getStationQueryXML(logistics_interface); String customer_code = "customer123"; //客户编码 //如果xml里面存在汉字 一定要在参数传递之前 进行编码 String urlEncoder = URLEncoder.encode(logistics_interface,"utf-8"); String result = post("logistics_interface="+urlEncoder+"&data_digest="+data_digest+"&customer_code="+customer_code,httpUrl); System.out.println(data_digest); System.out.println(result); }
如果xml里面含有中文。需要在传送之前进行URLEncoder.encode(param,”utf-8”) 进行编码。
相关文章推荐
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 解决HttpURLConnection请求时传中文参数乱码问题
- 关于Android使用HttpURLConnection传输含有中文JSON数据的报错、乱码问题
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
- 关于使用 ASIHTTPRequest URL中文参数乱码问题
- 使用HttpURlConnection 发送POST请求上传文件(带参数)
- 百度地图API 云存储·LBS.云 跨域问题解决方法——通过java.net.URLConnection发送HTTP请求
- java Struts2 使用HttpURLConnection进行post提交乱码
- java 使用HttpURLConnection发送数据简单实例
- java 中使用URL发送get,post请求乱码问题
- 关于使用 ASIHTTPRequest URL中文参数乱码问题
- 在Java中使用HttpURLConnection发送http客户端请求、服务器端返回信息、接受服务器端的信息
- httpurlconnection post 和GET 分别传递2个参数给后台,返回JSON 解决乱码问题
- Java发送http请求(httpUrlConnection)安卓客户端也可以使用
- HttpURLConnection使用POST方法参数乱码
- 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题
- JAVA使用原始HttpURLConnection发送POST数据
- 在JAVA中使用HttpURLConnection发送POST请求
- Java使用HttpURLConnection发送POST和GET请求