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. 垂直的SeekBar:VerticalSeekBar

    public class VerticalSeekBar extends AbsSeekBar { private Drawable mThumb; public interface OnSeekBa ...

  2. LR结构图

  3. Class文件结构

    各种不同平台的虚拟机与所有平台都统一使用的程序存储格式--字节码(ByteCode)是构成平台无关性的基石,除了平台无关性,虚拟机的另外一种中立特性--语言无关性正越来越被开发者所重视.在Java发展 ...

  4. 学习NGUI前的准备NGUI的相关信息

    学习NGUI前的准备NGUI的相关信息 第1章  学习NGUI前的准备 NGUI是Unity最重要的插件,在Unity资源商店(Asset Store)的付费排行榜中始终名列前茅,如图1-1所示.本章 ...

  5. 模拟 POJ 1068 Parencodings

    题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...

  6. html成绩单表格

    <!DOCTYPE html> <html> <head> <meta name="generator" content="HT ...

  7. python 代码片段15

    #coding=utf-8 ''' 如果运行时发生异常的话,解释器会查找相应的处理函数.要是在当前函数里没有 找到的话,它会将异常传递给上层的调用函数,看看那里能不能处理.如果在在最 外层还没有找到的 ...

  8. sql server 取随机行

    --从table_name中随机取n行 select top n * from table_name order by NEWID()

  9. Eclipse @override报错解决

    第一种解决方案: @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...

  10. QPushButton 与 QListWidget 的按键响应

    在Qt中添加按钮或表格控件时需要添加其按键响应,一般来说有两种方法,一种是直接实现其响应函数,第二种是自己写一个响应函数,然后用Qt的信号槽将它们连接起来.愚以为第一种方法更为简单一些. 声明这些控件 ...