in script,i use HttpWebRequest to get service from network.but it comes a mysterious problem. the source code:
string url = "http://apis.baidu.com/apistore/vop/baiduvopjson";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
print(request.Address);
request.Method = "get";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
string strValue = "";
strValue = Reader.ReadToEnd();
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:
string url = "http://apis.baidu.com/apistore/vop/baiduvopjson";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "get";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
string strValue = "";
strValue = Reader.ReadToEnd();
Console.WriteLine(strValue);
the result which is true in fact is as follows:
{"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?
个解答,截止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:
curl http://apis.baidu.com/apistore/vop/baiduvopjson
{"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.
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.