简体   繁体   中英

how to send a HTTP SEARCH request in HttpURLConnection on Android?

I want to know if it is possible to send SEARCH request (practically) through java.net.HttpURLConnection to HTTP-based URL.

I have read so many articles describing that how to send GET, POST, DELETE requests but I still haven't found any sample code which successfully performs SEARCH request.

here is sample code.

public static String HTTPSearch(String urlAddress, String... searchDataPair) {
    HttpParams myParams = new BasicHttpParams();

    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 30000);

    DefaultHttpClient hc = new DefaultHttpClient(myParams);
    ResponseHandler<String> res = new BasicResponseHandler();

    HttpSearch searchMethod = new HttpSearch (urlAddress);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
            putDataPair.length / 2);

    for (int i = 0; i < putDataPair.length; i += 2) {
        nameValuePairs.add(new BasicNameValuePair(searchDataPair[i],
                searchDataPair[i + 1]));
    }

    String response = "";
    try {
        searchMethod .setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = hc.execute(searchMethod , res);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

in this code, HttpSearch is error. please help me about HttpSearch.

First, i dont know whether my approach is best or not.

Im using restful codeigniter webservice . What i did to implement search is by using POST . I simply POST the search keyword and then my webservice will perform SQL Query to search that String (keyword) im my database.

根据文档 ,setRequestMethod()不允许使用SEARCH方法(这很有意义,因为据我所知SEARCH MS 。但是,您始终可以尝试使用setRequestMethod()看看会发生什么。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM