使用UrlFetchApp发送错误的请求 [英] bad request with UrlFetchApp

查看:141
本文介绍了使用UrlFetchApp发送错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是说明(来自 here ):


输入和继电器的状态可以通过向XML页面
state.xml发送
请求到端口80(或在安装程序中指定的端口)来监视。可以通过发送GET请求到端口80上的
相同页面(或在安装程序中指定的端口)来控制继电器。这可以通过将命令输入到Web浏览器的URL行来演示


请求当前状态:http://ip address/ state .xml b

$ b

如果在WebRelay-DualTM单元中启用了控制密码,并且
通过浏览器请求state.xml页面,浏览器将
提示用户输入密码。如果XML请求是从
另一个应用程序而不是浏览器发送的,则html请求将需要
包含使用base 64编码方案编码的密码。没有密码的
html头像如下所示:

  GET /state.xml?relay1State=1&noReply = 1 HTTP / 1.1(以两个\ r \ n结尾)

密码如下所示:

  GET /state.xml?relay1State=1&noReply=1 HTTP / 1.1(\r \\\
here)
授权:基本bm9uZTp3ZWJyZWxheQ ==(以两个\r\\\
结尾)

其中 bm9uZTp3ZWJyZWxheQ ==
用户名和密码的基础64编码版本和密码none:webrelay


代码:

 函数webRelay(){
/ / working url http://75.65.130.27/state.xml

var url ='http://75.65.130.27/';

var params = encodeURIComponent('state.xml');
Logger.log(params);

var headers = {
Authorization:Basic+ Utilities.base64Encode('none:webrelay')
};

var options =
{
method:get,
headers:headers
};

var state = UrlFetchApp.fetch(url + params,options);

Logger.log('1:'+ state);
Logger.log(parse(state));


函数解析(txt){
var doc = Xml.parse(txt,true);
}

非常感谢任何帮助。




    在授权标题中,您需要在基本之后留出空格。

    授权:基本+ Utilities.base64Encode(用户名+':'+密码)


  1. urlFetchApp.fetch()返回一个HTTP Response对象,提取解析内容。

    var result = UrlFetchApp.fetch(url,options);

    var state = result.getContentText();


  2. 您不会从您应该检查 result.getResponseCode()

  3. > .fetch()之后>,然后在继续解析之前处理错误。 $ b

    这就是说,我不断收到错误的请求:http://75.65.130.27/state .xml 错误,所以有些东西仍然不正确。这是一个HTTP 400响应,并且Google的服务器不会向脚本调试器返回任何内容以深入其中。您应该检查用户名和密码密码,尽管如果他们错了,我会期待401-Unauthorized的回应。我尝试了包含一个有效负载 relay1State = 2 ,并得到了相同的错误的请求结果。如果您可以捕获到您的服务器的HTTP请求,那么可能会发现格式错误的线索。这可能也是防火墙产生的结果。



    完成排序后,本教程应该有助于XML解析。



    以下是我的代码编辑:

     函数webRelay(){
    var url ='http://75.65.130.27/state.xml';
    var username =none;
    var password =webrelay;

    var header =
    {
    授权:Basic+ Utilities.base64Encode(username +':'+ password)
    }

    var options =
    {
    method:get,
    headers:headers
    };

    //在这里获取不良要求 - 检查用户名和密码密码
    var result = UrlFetchApp.fetch(url,options);
    var state = result.getContentText();

    //你应该检查state.getResponseCode()

    Logger.log('1:'+ state);
    Logger.log(parse(state));


    函数解析(txt){
    var doc = Xml.parse(txt,true);
    return doc; //返回结果
    }


    Looking for some help connecting to this service and returning the xml.

    Here are the instructions (from here):

    The state of the inputs and relays can be monitored by sending a request to port 80 (or port specified in setup) for the XML page state.xml. The relays can be controlled by sending GET requests to the same page on port 80 (or port specified in setup). This can be demonstrated by entering commands into the URL line of a web browser.

    Request the current state: http://"ip address"/state.xml

    ...

    If the control password is enabled in the WebRelay-DualTM unit and the state.xml page is requested through a browser, the browser will prompt the user for the password. If the XML request is sent from another application and not a browser, the html request will need to contain the password encoded using the base 64 encoding scheme. The html request header without the password looks like this:

    GET /state.xml?relay1State=1&noReply=1 HTTP/1.1 (Ends with two \r\n)
    

    The html request header with the password looks like this:

    GET /state.xml?relay1State=1&noReply=1 HTTP/1.1(\r\n here)
    Authorization: Basic bm9uZTp3ZWJyZWxheQ== (Ends with two \r\n)
    

    where bm9uZTp3ZWJyZWxheQ== is the base 64 encoded version of the user name and password none:webrelay

    Code:

    function webRelay(){
      //working url http://75.65.130.27/state.xml
    
      var url = 'http://75.65.130.27/';
    
      var params = encodeURIComponent('state.xml');
      Logger.log(params);
    
      var headers = {
        "Authorization" : "Basic" + Utilities.base64Encode('none:webrelay')
      };
    
      var options =
       {
         "method" : "get",
         "headers" : headers
       };
    
      var state = UrlFetchApp.fetch(url+params, options);
    
      Logger.log('1: '+state);
      Logger.log(parse(state));
    }
    
    function parse(txt) {
      var doc = Xml.parse(txt, true);
    }
    

    Any help is much appreciated.

    解决方案

    There are a couple of coding errors that you can easily take care of:

    1. In the Authorization header you need a space after "Basic".
      Authorization : "Basic " + Utilities.base64Encode(username+':'+password)

    2. urlFetchApp.fetch() returns an HTTP Response object, so you need to extract the contents for parsing.
      var result = UrlFetchApp.fetch(url, options);
      var state = result.getContentText();

    3. You aren't returning anything from your parse() function.

    4. You should check result.getResponseCode() after .fetch(), and handle errors before proceeding with parsing.

    That said, I keep getting Bad request: http://75.65.130.27/state.xml errors, so something is still not right. This is an HTTP 400 response, and google's servers don't return anything to the script debugger to dig into it. You should check the username & password, although I'd expect a 401-Unauthorized response if they were wrong. I tried including a payload of relay1State=2, and got the same Bad request result. If you can capture the HTTP Request hitting your server, there may be a clue to what is malformed. This could also be the result of a firewall.

    Once that's sorted, this tutorial should help with the XML Parsing.

    Here's my edit of your code:

    function webRelay(){
      var url = 'http://75.65.130.27/state.xml';
      var username = "none";
      var password = "webrelay";
    
      var headers =
      {
        Authorization : "Basic " + Utilities.base64Encode(username+':'+password)
      }
    
      var options =
      {
        "method" : "get",
        "headers": headers
      };
    
      // Getting "bad request" here - check the username & password
      var result = UrlFetchApp.fetch(url, options);
      var state=result.getContentText(); 
    
      // You should check state.getResponseCode()
    
      Logger.log('1: '+state);
      Logger.log(parse(state));
    }
    
    function parse(txt) {
      var doc = Xml.parse(txt, true);
      return doc;                            // Return results
    }
    

    这篇关于使用UrlFetchApp发送错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆