YoshionTwo 2015-07-13 10:06 采纳率: 0%
浏览 1967
已结题

关于http协议系统之间的交互

现在我们系统请求gps系统传一个String类型的参数,我们用的servlet,他们用的springMVC。我们的参数是以流的形式写过去的,但是gps那边说没参数没定义名称无法取值。我这菜鸟不知道怎么弄了。求助啊。

 public  String sendMessage(String sendMessage) throws Exception {

                 sendMessage=("xmlStr"+sendMessage).replaceFirst("UTF-8", "GBK");
                byte[] byt=sendMessage.getBytes("GBK");
           StringBuffer buffer = new StringBuffer();//接收返回字符串
           BufferedReader reader = null;
           OutputStream output =null;
           try {
               String strMessage = "";
               //获取文件路径
               String filePath=this.getClass().getResource("/").toString().substring(6)+"config.txt";
               Properties p=new Properties();
                FileReader in = null;
                String url="";
            //String 的内容
                //String sendMessage="<?xml version='1.0' encoding='GBK'?><MESSAGE ID='05-2013-10-30 10:10:00-00' TYPE='05' TIME='2013-10-30 10:10:00'><BODY><INFORMATION><DESTINATION><SHIP_ID></SHIP_ID ><SHIP_ID> </SHIP_ID></DESTINATION><BODY><USERNAME>a</USERNAME><PASSWORD>a</PASSWORD><TEXT></TEXT><IMAGE></IMAGE></BODY></INFORMATION></BODY></MESSAGE>";

               //测试用
               String url1="http://192.168.46.35:8080/BGpsWeb/gpsForeignInterface/shipLocation.do";
               URL uploadServlet = new URL( url1);

               HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet
                      .openConnection();
               // 设置连接参数
               servletConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
               servletConnection.setRequestProperty("context-type", "text/html");
               servletConnection.setRequestMethod("POST");
               servletConnection.setDoOutput(true);
               servletConnection.setDoInput(true);
               servletConnection.setAllowUserInteraction(true);
               //设置超时
               servletConnection.setConnectTimeout(30000);
               servletConnection.setReadTimeout(30000);

               // 开启流,写入XML数据
               output = servletConnection.getOutputStream();
           output.write(byt);

               output.flush();


               // 获取返回的数据
               InputStream inputStream=null;
               inputStream = servletConnection.getInputStream();
               if(inputStream!=null){
                   reader = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
                   while ((strMessage = reader.readLine()) != null) {
                       buffer.append(strMessage);
                   }
//                 if(buffer!=null)
//                     xmlStrToFile.storageXml(buffer.toString());
                                   }
           } catch (Exception e) {
               throw new RuntimeException(e);
           } finally {
               if (reader != null) {
                  reader.close();
               }
               if(output!=null){
                   output.close();
               }

           }

           return buffer.toString();
        }

  • 写回答

6条回答 默认 最新

  • 全栈极简 博客专家认证 2015-07-13 10:07
    关注

    你应该传json格式的字符串,轻量级,而且对方一看就懂。

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)