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条回复

· 添加您的回复

  • 排序:

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 · 分享
 

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.

【转】get a mysterious problem,when i use HttpWebRequest in unity c# script的更多相关文章

  1. python模块:logging

    # Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and ...

  2. python中的logger模块

    logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...

  3. python中的logger模块详细讲解

    logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...

  4. threading.RLock()

    # Copyright 2001-2017 by Vinay Sajip. All Rights Reserved.## Permission to use, copy, modify, and di ...

  5. A Study of WebRTC Security

    转自:http://webrtc-security.github.io/ A Study of WebRTC Security Abstract Web Real-Time Communication ...

  6. easily add files to META-INF in NetBeans

    http://georgeinfo.blog.163.com/blog/static/16368334120101019104044650/ ————————————————————————————— ...

  7. 部署的docker image总是太大,怎么办?

    sudo docker images REPOSITORY                        TAG                 IMAGE ID            CREATED ...

  8. Unity 5 Game Optimization (Chris Dickinson 著)

    1. Detecting Performance Issues 2. Scripting Strategies 3. The Benefits of Batching 4. Kickstart You ...

  9. Cross compiling coreutils and generate the manpages

    When we cross compiling coreutils, there is an problem of generating man pages, because the source s ...

随机推荐

  1. Mysql 对数字的格式化

    format函数:     格式化浮点数 format(number, length); Formats the number X to a format like '#,###,###.##', r ...

  2. 今年暑假不AC[HDU2037]

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. 终于把HDU的第一页做完了

    Today is worth to be commemorate for that I have eventually worked out all the questions in the firs ...

  4. 关于adb驱动

    Android设备(如手机)连接PC时所需要的驱动程序,一般Android设备连接WinXP是无需安装驱动的. adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb ...

  5. TYVJ P1036 统计数字 Label:坑!!!(用queue+map做出来的水)

    背景 NOIP2007年提高组第一题 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数 ...

  6. 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...

  7. iOS开发中代理使用出现的问题解决

    在给自定义的LHQTabBar设置代理的时候,定义的代理的属性的时候此时会报一个警告 我们需要遵守UITabBarDelegate的协议才行, 不过此时还有警告,警告已经变成了 此时我们需要在.m文件 ...

  8. iOS开发项目之一 [ 项目流程]

    项目流程 *人员配置 *客户端(iOS工程师,Android工程师) *前端 h5 *后台人员(php,java,net) *提供接口(请求地址.请求参数,请求方式,接口文档) *UI UE * 效果 ...

  9. Use auto keyword in C++11

    Now compile your program with g++ -std=c++ your_file.cpp -o main

  10. Qt 5.4 OpenCV 2.4.11 Win 32 Configuration

    Download CMake 2.8.2 Download OpenCV 2.4.11 Download Qt 5.4 Highly improtant note: The installation ...