代码之家  ›  专栏  ›  技术社区  ›  Swift

可以通过浏览器查看URL,但不能使用curl

  •  2
  • Swift  · 技术社区  · 14 年前

    今天我在开发一个应用程序时遇到了一个有趣的问题。希望有人知道这是为什么/如何调整我的工作流程!

    背景:
    我正在写一份申请表,帮助大学里的学生互相联系。基本工作流程如下:

    1. 用户注册服务
    2. 应用程序使用curl在大学目录中查询其名称。
    3. 将他们的联系信息存储在数据库表中

    我的考试地点是罗格斯大学的目录( http://www.acs.rutgers.edu/directory ) 我可以通过浏览器访问该服务(发布到 http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results 但是如果我试图通过curl发布相同的信息,我会得到404错误。

    注: 如果我直接从浏览器打开该URL,而不使用它们的表单提交数据,我会得到同样的错误。

    代码:
    以下是他们在目录站点上使用的代码:

    <fieldset>
    <legend>Search For People</legend>
    <div style="width:50%;margin-left:auto;margin-right:auto;">
    <form method="post" action="http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results" name="thisform" onkeyup="highlight()" onclick="highlight()">
        <p>
            <label for="p_name_last">Last Name:&nbsp;&nbsp;[Required]</label><br>
            <input tabindex="1" accesskey="L" class="required" type="text" id="p_name_last" name="p_name_last" size="25" maxlength="23">
        </p> 
        <p>
            <label for="p_name_first">First Name:&nbsp;&nbsp;[or initial]</label><br> 
            <input tabindex="2" accesskey="f" type="text" id="p_name_first" name="p_name_first" size="25" maxlength="23"> 
        </p>
        <input tabindex="3" type="submit" value="Search">&nbsp;&nbsp;
    </form>
    </div>
    

    下面是我用来提供服务的代码:

    <?php
    $p_name_last = "doe";
    $p_name_first = "";
    $curlPost = 'p_name_last='  . urlencode($p_name_last) . '&p_name_first=' . urlencode($p_name_first) . '&SUBMIT=Search';
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results');
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    echo "<pre>";
    print_r($result); 
    ?>
    

    如果您有任何想法或建议,我们将不胜感激!

    谢谢,
    迈克

    4 回复  |  直到 6 年前
        1
  •  3
  •   brabster    14 年前

    Web服务器可以选择在处理请求后发送什么响应。这就是为什么你看到404——那里有一个资源,但是它以一种非直觉的方式响应。

    不管怎样,我可以用curl在这个页面上发布并得到回复;

    curl http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results --data-urlencode p_name_last=test --data-urlencode p_name_first=test

    如果我发布的是你的提交。参数我得到404。尝试只使用两个名称参数。


    更新:实际上,只要存在first name参数,就可以只发送last name参数-first name参数可以为空。

        2
  •  1
  •   J Griffiths    12 年前

    问题似乎是窗体的输入元素

    <input tabindex="3" type="submit" value="Search">
    

    不包含name属性,因此不在表单的post数据中发送 SUBMIT=search 或者至少不应该这样,根据 http://www.w3schools.com/tags/att_input_name.asp ,其中规定

    “只有具有name属性的表单元素在提交表单时才会传递其值。”

    我想您的尝试导致了404响应,因为接受您请求的脚本从不期望 SUBMIT 变量,这就是为什么接受的解决方案有效。

        3
  •  0
  •   snowfox    6 年前

    检查浏览器代理设置。 我也有同样的问题。原来是我的代理设置导致的,它将请求定向到代理服务器。

        4
  •  -1
  •   Hydrino    14 年前

    确实页面 http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results 不存在。 您已重定向到新地址: http://eas.rutgers.edu ,浏览器可以遵循重定向,但curl可能有问题。

    您试图访问的网页已移动!

    新地址: 网址:http://eas.rutgers.edu

    请记住更新您的书签。

    此页面将在10秒内自动重定向。 如果重定向失败,请单击 网址:http://eas.rutgers.edu 继续。

    您试图访问的网页已移动!

    新地址: 网址:http://eas.rutgers.edu

    请记住更新您的书签。

    此页面将在10秒内自动重定向。 如果重定向失败,请单击 网址:http://eas.rutgers.edu 继续。