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
{
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;
}
}