`
zwhc
  • 浏览: 258366 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

电信彩信附件,webservice HttpURLConnection

    博客分类:
  • java
阅读更多
电信彩信附件,webservice HttpURLConnection

原先用 gSOAP 开发过一个,所发送的彩信(电信),附件中的图片可以打开。
生产环境中必须用 java,用 axis 开发出来的,所发送的彩信,附件中的图片无法打开。

解决方式:
1、用 wireshark 抓取正常的彩信包;
2、用 HttpURLConnection 直接发送:

    private void testMMS(String boundary, String start, byte[] requestData) throws Exception{
    	HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setRequestProperty("User-Agent", "kSOAP/2.0");
        connection.setRequestProperty("SOAPAction", "");
        connection.setRequestProperty("Content-Type", "multipart/related; charset=utf-8; " +
        		"boundary=\""+boundary+"\"; " +
        		"type=\"text/xml\"; start=\"<"+start+">\"");
        connection.setRequestProperty("Connection", "close");
        connection.setRequestProperty("Content-Length", "" + requestData.length);
        connection.setRequestMethod("POST");
        connection.connect();
        OutputStream os = connection.getOutputStream();
        System.out.println( "245_requestData.length:" + requestData.length );
        os.write(requestData, 0, requestData.length);
        os.flush();
        os.close();

        requestData = null;
        InputStream is;
        try {
            connection.connect();
            is = connection.getInputStream();
        } catch (IOException e) {
        	e.printStackTrace();
            is = connection.getErrorStream();
            System.out.println("is:" + is);
            if (is == null) {
                connection.disconnect();
                throw (e);
            }
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[256];
        while (true) {
            int rd = is.read(buf, 0, 256);
            if (rd == -1)
                break;
            bos.write(buf, 0, rd);
        }
        bos.flush();
        buf = bos.toByteArray();
        responseDump = new String(buf);
        System.out.println("responseDump:" + responseDump);

        is.close();
        is = new ByteArrayInputStream(buf);
    }


这段代码,是从 kSOAP 里扒出来的。

在研究过程中,因为对 webservice 的附件不太了解,还参考了 axis 的源码。
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics