首页 技术 正文
技术 2022年11月11日
0 收藏 663 点赞 3,230 浏览 1872 个字

in script,i use HttpWebRequest to get service from network.but it comes a mysterious problem. the source code:

  1. string url = "http://apis.baidu.com/apistore/vop/baiduvopjson";
  2. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  3. print(request.Address);
  4. request.Method = "get";
  5. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  6. Stream s = response.GetResponseStream();
  7. StreamReader Reader = new StreamReader(s, Encoding.UTF8);
  8. string strValue = "";
  9. strValue = Reader.ReadToEnd();
  10. print (strValue);

the result is like(i have copy the html to a html file):

but the mysterious problem comes.when i use this code in Visual studio c# project,here is the source code:

  1. string url = "http://apis.baidu.com/apistore/vop/baiduvopjson";
  2. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  3. request.Method = "get";
  4. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  5. Stream s = response.GetResponseStream();
  6. StreamReader Reader = new StreamReader(s, Encoding.UTF8);
  7. string strValue = "";
  8. strValue = Reader.ReadToEnd();
  9. Console.WriteLine(strValue);

the result which is true in fact is as follows:

  1. {"errNum":300202,"errMsg":"Missing apikey"}

do some one know what mistake do i make in unity project? why can i get the ture httpweb response?

1.jpg (21.9 kB) 评论 

1条回复

· 添加您的回复

  • 排序:

个解答,截止elenzil · 05月10日 18:42

the problem is not your use of HTTPWebRequest, the problem is your use of the remote service. if you use command-line “curl” with the same URL, you get the same response:

  1. curl http://apis.baidu.com/apistore/vop/baiduvopjson
  2. {"errNum":300202,"errMsg":"Missing apikey"}

or even just visit http://apis.baidu.com/apistore/vop/baiduvopjson in a browser – same response.

it seems like you should be providing an API key somehow. maybe as a form parameter, maybe as a URL parameter, header param, etc, etc. you should look up the docs for whatever service that is.

 评论 ·  隐藏 1 · 分享ceo1207· 05月11日 01:13 0 

I have found the point of my problem….The string of request method should be uppercase….omg,the mono .net has such a slight difference with .net framework. Anyway,thanks for your reply.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920