加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

HttpURLConnection "no content-type" 客户端解决办法

(2010-01-06 11:56:07)
标签:

杂谈

分类: 软件开发
Exception in thread "main" java.net.UnknownServiceException: no content-type
 at java.net.URLConnection.getContentHandler(URLConnection.java:1192)
主要原因是服务器端没有返回content-type字段,而且SUN JDK作为客户端时没有进行if failed, then guess and try的操作
有两种解决办法:
1. 在服务器端Tomcat 6.0\conf\web.xml加上
    <mime-mapping>
        <extension>wsdl</extension>
        <mime-type>application/xml</mime-type>
    </mime-mapping>
2. 在客户端进行guess and try:
 
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.util.Date;
import sun.net.www.protocol.http.Handler;
public class GenericContentTypeHttpURLConnection extends sun.net.www.protocol.http.HttpURLConnection {
    public static void main(String[] args) {
        try {
            URL url1 = new URL("file", "localhostA", 89, "D://in.csv");
            URL url2 = new URL("file://localhost:8080/D://in.csv");
            URL url3 = new URL("file://192.168.0.22:8080/axis2/ManageEstablishmentGES.wsdl");
            URLConnection con = url3.openConnection();
            System.out.println("Date : " + new Date(con.getDate()));
            System.out.println("Content Type : " + con.getContentType());
            System.out.println("Content Length : " + con.getContentLength());
            InputStream in = con.getInputStream();
            int j = 0;
            while ((j = in.read()) != -1) {
                System.out.print((char) j);
            }
            in.close();
        } catch (Exception e) {
            System.out.println("Exp in catch : " + e);
        }
    }
    public static Field getField(Class clazz, Class type, boolean instance) {
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            boolean isStatic = Modifier.isStatic(fields[i].getModifiers());
            if (instance != isStatic && fields[i].getType().equals(type)) {
                fields[i].setAccessible(true);
                return fields[i];
            }
        }
        return null;
    }
    public static void install4Common() {
        URLStreamHandlerFactory factory = new java.net.URLStreamHandlerFactory() {
            public URLStreamHandler createURLStreamHandler(String protocol) {
                if (protocol.equals("http")) {
                    URLStreamHandler handler = new URLStreamHandler() {
                        @Override
                        protected URLConnection openConnection(URL u) throws IOException {
                            return new GenericContentTypeHttpURLConnection(u, null);
                        }
                    };
                    return handler;
                }
                return null;
            }
        };
        URL.setURLStreamHandlerFactory(factory);
    }
    public static void install4Eclipse() {
        URLStreamHandlerFactory factory = new java.net.URLStreamHandlerFactory() {
            public URLStreamHandler createURLStreamHandler(String protocol) {
                if (protocol.equals("http")) {
                    URLStreamHandler handler = new URLStreamHandler() {
                        @Override
                        protected URLConnection openConnection(URL u) throws IOException {
                            return new GenericContentTypeHttpURLConnection(u, null);
                        }
                    };
                    return handler;
                }
                return null;
            }
        };
        try {
            Field factoryField = getField(URL.class, URLStreamHandlerFactory.class, false);
            // if (factoryField == null) {
            //            throw new Exception("Could not find URLStreamHandlerFactory field"); //$NON-NLS-1$
            // }
            URLStreamHandlerFactory fac = (URLStreamHandlerFactory) factoryField.get(null);
            if (fac instanceof org.eclipse.osgi.framework.internal.protocol.StreamHandlerFactory) {
                org.eclipse.osgi.framework.internal.protocol.StreamHandlerFactory eclipseStreamHandlerFactory = (org.eclipse.osgi.framework.internal.protocol.StreamHandlerFactory) fac;
                Field parentFactoryField = getField(org.eclipse.osgi.framework.internal.protocol.StreamHandlerFactory.class,
                        URLStreamHandlerFactory.class, true);
                parentFactoryField.set(eclipseStreamHandlerFactory, null);
                parentFactoryField.set(eclipseStreamHandlerFactory, factory);
            } else {
                // for common usecase, set the field of URL.class as StreamHandlerFactory=null
                factoryField.set(null, factory);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        // URL.setURLStreamHandlerFactory(factory);
    }
    public GenericContentTypeHttpURLConnection(URL arg0, Proxy arg1, Handler arg2) {
        super(arg0, arg1, arg2);
        // TODO Auto-generated constructor stub
    }
    public GenericContentTypeHttpURLConnection(URL arg0, Proxy arg1) {
        super(arg0, arg1);
        // TODO Auto-generated constructor stub
    }
    public GenericContentTypeHttpURLConnection(URL arg0, String arg1, int arg2) {
        super(arg0, arg1, arg2);
        // TODO Auto-generated constructor stub
    }
    @Override
    public Object getContent() throws IOException {
        // TODO Auto-generated method stub
        Object content = null;
        try {
            content = super.getContent();
        } catch (Exception e) {
            // e.printStackTrace();
            sun.net.www.content.text.Generic generic = new sun.net.www.content.text.Generic();
            content = generic.getContent(this);
        }
        return content;
    }
}
http://s8/middle/702c40664963659f9bbb7&690"no content-type" 客户端解决办法" />
 
 
 
http://s6/middle/702c4066496365a076ad5&690"no content-type" 客户端解决办法" />

0

阅读 收藏 喜欢 打印举报/Report
前一篇:php与zend
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有