谁能帮我这一小段C#转成Java代码

muyebo 2009-07-13 09:35:05
using System;
using System.Text;
using System.IO;
using System.Net;


namespace 信息提取
{
public partial class class1
{
string GetWebContent(string Url)
{
string strResult = "";
try
{
WebRequest request = WebRequest.Create(Url);
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
WebResponse response = request.GetResponse();
Stream streamReceive = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamReceive, Encoding.GetEncoding("GB2312"));
strResult = streamReader.ReadToEnd();
}
catch
{
}
return strResult;
}
}
}
...全文
64 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyebo 2009-07-14
  • 打赏
  • 举报
回复
这个解析起来还真不容易
tg008007x3 2009-07-14
  • 打赏
  • 举报
回复
学习中!
East271536394 2009-07-14
  • 打赏
  • 举报
回复
package url;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class TestUrl {
public TestUrl() {
}

public static void main(String[] argv) {
URL url = null;
try {
url = new URL("http://www.sina.com.cn");
URLConnection urlConn = url.openConnection();
DataInputStream din = new DataInputStream(urlConn.getInputStream());
byte[] buf = new byte[1024];
int length = din.available();
System.out.println("######" + length + "######");
int readlength = 0;
while (true) {
readlength += din.read(buf);
System.out.print(new String(buf));
if (readlength >= length) {
break;
}
}
System.out.println("\n######" + readlength + "######");
} catch (MalformedURLException ex) {
} catch (IOException ex) {
}
}
}
悠云guo 2009-07-13
  • 打赏
  • 举报
回复
解析得自己做,没有现成API
muyebo 2009-07-13
  • 打赏
  • 举报
回复
上面那段已经解决了,能不能再问一下
            WebBrowser webb = new WebBrowser();
webb.Navigate("about:blank");
HtmlDocument htmldoc = webb.Document.OpenNew(true);
htmldoc.Write(strDetail);
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
foreach (HtmlElement tr in htmlTR)
{
.....
}
悠云guo 2009-07-13
  • 打赏
  • 举报
回复
package url;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class TestUrl {
public TestUrl() {
}

public static void main(String[] argv) {
URL url = null;
try {
url = new URL("http://www.sina.com.cn");
URLConnection urlConn = url.openConnection();
DataInputStream din = new DataInputStream(urlConn.getInputStream());
byte[] buf = new byte[1024];
int length = din.available();
System.out.println("######" + length + "######");
int readlength = 0;
while (true) {
readlength += din.read(buf);
System.out.print(new String(buf));
if (readlength >= length) {
break;
}
}
System.out.println("\n######" + readlength + "######");
} catch (MalformedURLException ex) {
} catch (IOException ex) {
}
}
}


类似代码
ChrisAK 2009-07-13
  • 打赏
  • 举报
回复
楼主这问题应该搬到java版去问@_@

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧