簡體   English   中英

HttpPost將GET發送到服務器

[英]HttpPost send GET to the server

我的問題已解決,服務器的所有者刪除了一些代碼,以檢查發送POST / GET方法的基礎是否正常,並且工作正常。

我仍然不知道問題出在哪里,但是肯定不是我的問題。


我正在嘗試將POST方法發送到服務器,但事實證明服務器改為使用GET方法。 我們在iPhone上運行了它,因此服務器是正確的。

這是我編寫的用於發送數據的代碼:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://myurl.nl/casus");
    httppost.setHeader("Authorization", "xxxxxxxxxxxxxxx");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        //casus
        nameValuePairs.add(new BasicNameValuePair("casusid", casus.id+""));

        String woorden = "[";
        for(int i = 0; i < words.length; i++){
            woorden += "\""+words[i]+"\"";
            if(i != words.length-1){
                woorden += ",";
            }
        }
        woorden += "]";
        nameValuePairs.add(new BasicNameValuePair("woorden", woorden));
        //info
        SharedPreferences sharedPref = getSharedPreferences("omapp", MODE_PRIVATE);
        String age = sharedPref.getString("age", "");
        String sex = sharedPref.getString("sex", "");
        nameValuePairs.add(new BasicNameValuePair("leeftijdscategorie", age));
        nameValuePairs.add(new BasicNameValuePair("geslacht", sex));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        Log.d("json", responseBody);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

PHP代碼:

    // Service token authorization.
    require_once('../classlib/Auth.php');
    $auth = new Auth;
    if ( !$auth->valid() ) {
        header('HTTP/1.1 401 Unauthorized');
        exit;
    }

    // Setup casussen.
    require_once('../classlib/PDOFactory.php');
    require_once('../classlib/Casussen.php');
    $casussen = new Casussen;
    $casussen->setPDO(PDOFactory::create());

    $rs = (object) array();
    switch ( $_SERVER['REQUEST_METHOD'] ) {

        case 'GET': {
            mail('sdgsdgsdg@gmail.com', 'The get method was called', print_r($_SERVER,1));
            $fromcasusid = !empty($_GET['fromcasusid']) ? (int) $_GET['fromcasusid'] : null;
            $rs->casussen =$casussen->get($fromcasusid);
            break;
        }

        case 'POST': {
            mail('jsdgsdgsn@gmail.com', 'The post method was called', print_r($_SERVER,1));
            $casusreactie = json_decode(file_get_contents("php://input"));
            try {
                $casussen->post($casusreactie);
            } catch ( Exception $e ) {
                $rs->error = (object) array(
                    'code' => $e->getCode(),
                    'message' => $e->getMessage(),
                );
            }
            break;
        }

        default: {
            header('HTTP/1.1 405 Method Not Allowed');
            exit;
        }

    } // switch

    header('Content-type: application/json; charset=utf-8');
    exit(json_encode($rs));

一種鍛煉

從doGet內部調用doPost方法

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{

doPOst(request, response)
}

我也有類似的問題。 我可以通過添加“ www”來修復它。 到網址。 但是我想知道這有什么不同,我已經成功使用jQuery ajax請求測試了服務器處理,而沒有使用“ www”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM