package Study;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;public class YahooSearchGateway extends HttpServlet{
private static final String YAHOO_SEARCH_URL ="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=your_app_id&type=all";
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String url =YAHOO_SEARCH_URL+"&"+request.getQueryString();
System.out.println("url="+url);
HttpURLConnection con =(HttpURLConnection)new URL(url).openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
System.out.println(con);
response.setStatus(con.getResponseCode());
response.setContentType("text/xml");
BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String input =null;
OutputStream responseOutput =response.getOutputStream();
while ((input =reader.readLine()) !=null){
responseOutput.write(input.getBytes());
}
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
processRequest(request,response);
}
}
返回:
url=http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=your_app_id&type=all&query=hello&results=10
weblogic.net.http.SOAPHttpURLConnection:http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=your_app_id&type=all&query=hello&results=10
<Apr 25, 2006 10:05:24 AM CST> <Error> <HTTP> <BEA-101019> <[ServletContext(id=16325775,name=Mytest,context-path=/Mytest)] Servlet failed with IOException
java.net.UnknownHostException: api.search.yahoo.com
at java.net.InetAddress.getAllByName0(InetAddress.java:1016)
at java.net.InetAddress.getAllByName0(InetAddress.java:981)
at java.net.InetAddress.getAllByName(InetAddress.java:975)
at weblogic.net.http.HttpClient.openServer(HttpClient.java:274)
at weblogic.net.http.HttpClient.openServer(HttpClient.java:340)
at weblogic.net.http.HttpClient.<init>(HttpClient.java:135)
at weblogic.net.http.HttpURLConnection.getHttpClient(HttpURLConnection.java:138)
at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:345)
at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:28)
at weblogic.net.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:798)
at Study.YahooSearchGateway.processRequest(YahooSearchGateway.java:18)
at Study.YahooSearchGateway.doGet(YahooSearchGateway.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
> 其中的url我在IE中发现可以返回。