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. HDU 4349 Xiao Ming's Hope lucas定理

    Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB  Description Xiao Ming likes counting nu ...

  2. loj 1025(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25902 #include<iostream> #inc ...

  3. Tomcat安装及配置

    用来进行web开发的工具有很多,Tomcat是其中一个开源的且免费的java Web服务器,是Apache软件基金会的项目.电脑上安装配置Tomcat的方法和java有些相同,不过首先需要配置好jav ...

  4. 浅析C#中的Attribute(转)

    最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAttribute [Attribu ...

  5. TFS2012团队管理基本配置及基础使用方法

    本文介绍如何在VS2012中使用微软提供的TFS2012服务器进行团队协作开发,免费默认只支持5用户,主要分为两大步服务器配置跟客户端配置. 转载请标注:http://www.kwstu.com/Ar ...

  6. Get function name by address in Linux

    Source file: 1: #define __USE_GNU //import! 2: #include <dlfcn.h> 3: #include <stdio.h> ...

  7. 十个JavaScript中易犯的小错误,你中了几枪?

    序言 在今天,JavaScript已经成为了网页编辑的核心.尤其是过去的几年,互联网见证了在SPA开发.图形处理.交互等方面大量JS库的出现. 如果初次打交道,很多人会觉得js很简单.确实,对于很多有 ...

  8. 【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1877 费用流做多了,此题就是一眼题. 拆点表示只能经过一次,容量为1,费用为0. 然后再连边即可,跑 ...

  9. redux-actions源码解读

    一.什么是redux-actions redux-actions是一个简化action和reducer创建的一个封装库,里面有5个js文件, createAction.js handleAction. ...

  10. Quartz与Spring整合进行热部署的实现(一)

    先来几张实现图 任务管理页 新建任务管理.目前实现叫简单的需求...若各位同学要实现复杂的设计...quartz都有提供强大的支持.小弟目前的需求做到这已经够用了. 接下来.我们如何实现quartz的 ...