//获得页面内容,然后再将内容发送到别人指定邮箱吧!!
public static String getUrlContent(String s, String s1)
{
try
{
URL url = new URL(URLEncoder.encode(s));
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(url.openStream()));
String s2 = "";
String s4 = "";
for(String s3 = bufferedreader.readLine(); s3 != null; s3 = bufferedreader.readLine())
s4 = s4 + s3 + "\n"; s4 = StringParse.replaceString(s4, "/image", "/IMAGE", true);
s4 = StringParse.replaceString(s4, "/IMAGE", s1, true);
//
return s4;
}
catch(Exception exception)
{
return s + "error:" + exception.getMessage();
}
}
public static String getUrlContent(String s, String s1)
{
try
{
URL url = new URL(URLEncoder.encode(s));
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(url.openStream()));
String s2 = "";
String s4 = "";
for(String s3 = bufferedreader.readLine(); s3 != null; s3 = bufferedreader.readLine())
s4 = s4 + s3 + "\n"; s4 = StringParse.replaceString(s4, "/image", "/IMAGE", true);
s4 = StringParse.replaceString(s4, "/IMAGE", s1, true);
//
return s4;
}
catch(Exception exception)
{
return s + "error:" + exception.getMessage();
}
}
解决方案 »
- 请教一下关于if(ok(xxx))表达式
- 有关list和linkedlist的一个小问题
- 问一个关于异常的问题
- JSpinner的SpinnerDateModel能否设置成24小时的
- 小弟刚学JAVA,有个非常简单的问题问一下。
- 内部类中的sayHello方法中为什么不能访问abc
- 计算程序运行时间的问题?
- 如何转义英文单引号
- 如何使用InputStream中的mark(),reset()函数,我一直没搞明白!
- 请问如何在Java中调用一个我自己编写的exe程序呢?谢谢!
- 关于bean的问题
- weblogic7如何配置默认文档,比如index.html,default.htm,index.jsp等,谢谢
import java.net.*;
import java.io.*;InputStream in ;
String s ="";
String sCurrentLine =null;
URL url =new URL("你的天气预报的网址(可带参数)例如:www.tianqi.com?city=shanghai");
HttpURLConnection cnn =(HttpURLConnection)url.openConnection();
cnn.connect();
in =cnn.getInputStream();
BufferedReader buffer =new BufferedReader(new InputStreamReader(in));
while ((sCurrentLine =buffer.readLine()) != null) {
s+=sCurrentLine;
}
然后分析字符串 s 找到你要的信息就可以发给可户端了。
-------------------------------------------------------------------------
如果要用 js 就需要在 IE 中使用 xml 了,其实也很简单,代码如下:
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET","你的天气预报的网址(可带参数)例如:www.tianqi.com?city=shanghai",false);
xmlHttp.send();
var s =xmlHttp.responseText;
然后分析字符串 s 找到你要的信息就可以了。------------------------------------------------------------------------
至于如何分析 获得的字符串可以有很多方法,正则表达式是一种方法。
//file: CanisMinor.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;public class CanisMinor extends JFrame
{
protected JEditorPane mEditorPane;
protected JTextField mURLField; public CanisMinor(String urlString)
{
super("CanisMinor v1.0");
createUI(urlString);
setVisible(true);
} protected void createUI(String urlString)
{
setSize(500, 600);
center( );
Container content = getContentPane( );
content.setLayout(new BorderLayout( ));
// add the URL control
JToolBar urlToolBar = new JToolBar( );
mURLField = new JTextField(urlString, 40);
urlToolBar.add(new JLabel("Location:"));
urlToolBar.add(mURLField);
content.add(urlToolBar, BorderLayout.NORTH);
// add the editor pane
mEditorPane = new JEditorPane( );
mEditorPane.setEditable(false);
content.add(new JScrollPane(mEditorPane), BorderLayout.CENTER);
// open the initial URL
openURL(urlString);
// go to a new location when enter is pressed in the URL field
mURLField.addActionListener(new ActionListener( )
{
public void actionPerformed(ActionEvent ae)
{
openURL(ae.getActionCommand( ));
}
}); // add the plumbing to make links work
mEditorPane.addHyperlinkListener(new LinkActivator( ));
// exit the application when the window is closed
addWindowListener(new WindowAdapter( )
{
public void windowClosing(WindowEvent e) { System.exit(0); }
});
} protected void center( )
{
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize( );
Dimension us = getSize( );
int x = (screen.width - us.width) / 2;
int y = (screen.height - us.height) / 2;
setLocation(x, y);
} protected void openURL(String urlString)
{
try
{
URL url = new URL(urlString);
mEditorPane.setPage(url); mURLField.setText(url.toExternalForm( ));
}
catch (Exception e)
{
System.out.println("Couldn't open " + urlString + ":" + e);
}
} class LinkActivator implements HyperlinkListener
{
public void hyperlinkUpdate(HyperlinkEvent he)
{
HyperlinkEvent.EventType type = he.getEventType( );
if (type == HyperlinkEvent.EventType.ENTERED)
mEditorPane.setCursor(
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
else if (type == HyperlinkEvent.EventType.EXITED)
mEditorPane.setCursor(Cursor.getDefaultCursor( ));
else if (type == HyperlinkEvent.EventType.ACTIVATED)
openURL(he.getURL().toExternalForm( ));
}
} public static void main(String[] args)
{
String urlString = "http://www.csdn.net";
if (args.length > 0)
urlString = args[0];
new CanisMinor(urlString);
}
}