求把这段JAVA代码转成C#的,谢谢!

zhlin118 2011-10-24 06:17:26

String apikey = "111111111111111111111111111111";
String isbnUrl = "http://api.douban.com/book/subject/isbn/";

public static void main(String[] args) throws Exception {
//requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
//eg:http://api.douban.com/book/subject/isbn/9787111298854?apikey=111111111111111111114
ISBN isbnTest = new ISBN();
String isbnNo = "9787111298854";
String xml = isbnTest.fetchBookInfoByXML(isbnNo);
System.out.println(xml);
}

/**
* 从根据isbn号从豆瓣获取数据。已经申请apikey,每分钟最多40次请求,足够用。
* @param isbnNo
* @return
* @throws IOException
*/
public String fetchBookInfoByXML(String isbnNo) throws IOException {
String requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}

br.close();
return sb.toString();
}

...全文
86 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
keepfool 2011-10-25
  • 打赏
  • 举报
回复
手帮你写的。
keepfool 2011-10-25
  • 打赏
  • 举报
回复

using System;
using System.Text;

internal string apikey = "111111111111111111111111111111";
internal string isbnUrl = "http://api.douban.com/book/subject/isbn/";

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws Exception
static void Main(string[] args)
{
//requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
//eg:http://api.douban.com/book/subject/isbn/9787111298854?apikey=111111111111111111114
ISBN isbnTest = new ISBN();
string isbnNo = "9787111298854";
string xml = isbnTest.fetchBookInfoByXML(isbnNo);
Console.WriteLine(xml);
}

/// <summary>
/// 从根据isbn号从豆瓣获取数据。已经申请apikey,每分钟最多40次请求,足够用。 </summary>
/// <param name="isbnNo"> </param>
/// <returns> </returns>
/// <exception cref="IOException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public String fetchBookInfoByXML(String isbnNo) throws IOException
public virtual string fetchBookInfoByXML(string isbnNo)
{
string requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
InputStream @is = conn.inputStream;
InputStreamReader isr = new InputStreamReader(@is, "utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

string line = null;
while ((line = br.readLine()) != null)
{
sb.Append(line);
}

br.close();
return sb.ToString();
}
kkbac 2011-10-25
  • 打赏
  • 举报
回复


google首页怎么转的?
freemangood 2011-10-25
  • 打赏
  • 举报
回复
标记下,很有用啊。
zhlin118 2011-10-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 keepfool 的回复:]

手帮你写的。
[/Quote]

你这段代码变化不大啊,下面的有些对象,C#里都不是这些,JAVA里才有的啊,所以还是不能用,不过还是谢谢你出力了。
zhlin118 2011-10-25
  • 打赏
  • 举报
回复
谢谢1楼,经测试,可用。
宝_爸 2011-10-24
  • 打赏
  • 举报
回复
我用google的首页进行测试的

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
string apikey = "111111111111111111111111111111";
string isbnUrl = "http://api.douban.com/book/subject/isbn/";

static void Main(string[] args)
{
//requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
//eg:http://api.douban.com/book/subject/isbn/9787111298854?apikey=111111111111111111114
Program isbnTest = new Program();
string isbnNo = "9787111298854";
string xml = isbnTest.fetchBookInfoByXML(isbnNo);
Console.WriteLine(xml);
}

public string fetchBookInfoByXML(string isbnNo)
{
//string requestUrl = isbnUrl + isbnNo + "?apikey=" + apikey;
string requestUrl = "http://www.google.com";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode.ToString() != "OK")
{
Console.WriteLine(response.StatusDescription.ToString());
return "";
}

StreamReader myreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseText = myreader.ReadToEnd();
return responseText;

}
}
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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