【问题标题】:Http Post not postingHttp Post 未发布
【发布时间】:2025-11-27 22:20:02
【问题描述】:

因此,我尝试向网站发送发布请求,以搜索特定内容,例如游戏网站上的游戏。问题是每当我执行帖子时,它似乎只返回将帖子值放入搜索栏中的主网站,而不是结果页面。我会打印出整个日志,但它非常长,所以我只写了放置字符串的部分,但它仍然是主页。

public class Search extends Fragment {

/*Fragment Variables*/
private String[] results = new String[]{};
private EditText searchEdit;
private View view;

/*AsyncTask Variables*/
private Document doc;
private String urlString = "http://www.vogella.com";
private String title;
private String gamespotSearchId = "siteSearch";
private List<NameValuePair> list = new ArrayList<NameValuePair>(1);


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_search, container, false);
    searchEdit = (EditText) view.findViewById(R.id.searchEditText);
    new BackGroundTask().execute();
    return view;
}
class BackGroundTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        HttpPost httpPost = new HttpPost("http://www.gamespot.com");
        list.add(new BasicNameValuePair("q", "final fantasy"));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(list));
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(httpPost);
                // writing response to log
                readStream(response.getEntity().getContent());
            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();

            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // Making HTTP Request

        return null;
    }

    private void readStream(InputStream inputStream) {
        BufferedReader buf = null;
        try{
            buf = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while((line = buf.readLine())!= null){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(buf != null){
                try{
                    buf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    protected void onPostExecute(String result) {
        Log.d("onPostExecute", "just set the text view");
        super.onPostExecute(result);
    }

}

}

日志

I/System.out( 7052):         </div>
I/System.out( 7052):       </div>
I/System.out( 7052):     </div>
I/System.out( 7052):   </section>
I/System.out( 7052):   <div class="container">
I/System.out( 7052):     <div class="masthead-container masthead-container--flex masthead-            container--buffer">
I/System.out( 7052):       <!-- Logo -->
I/System.out( 7052):               <h1 id="masthead-logo" class="masthead-logo">
I/System.out( 7052):           <a href=/ data-click-track-id=coreHeaderFrontDoor  class="logo-site">
I/System.out( 7052):             <span class="logo-site--short"></span>
I/System.out( 7052):             <span class="logo-site--full"></span>
I/System.out( 7052):           </a>
I/System.out( 7052):         </h1>
I/System.out( 7052):       
I/System.out( 7052):       <!-- Search -->
I/System.out( 7052):               <form action="/search/" method="get" id="siteSearch" class="masthead-search search">
I/System.out( 7052):           <fieldset>
I/System.out( 7052):             <i class="icon icon-search"></i>
I/System.out( 7052):             <input type="text" id="search-main" name="q"
I/System.out( 7052):                               placeholder="Search GameSpot"
I/System.out( 7052):                           autocomplete="off" class="search-input ready" data-search-index="game" value="final fantasy" />
I/System.out( 7052):           </fieldset>
I/System.out( 7052):         </form>
I/System.out( 7052):       
I/System.out( 7052):       <nav id="masthead-nav" role="navigation" class="masthead-nav">
I/System.out( 7052):         <ul class="nav-bar">
I/System.out( 7052):           <li class="nav-bar__item dropnav js-dropnav">
I/System.out( 7052):             <a href=/new-games/ data-click-track-id=coreHeaderNewGames >Games<i class="icon icon-caret-down"></i></a>
I/System.out( 7052):             <i class="icon icon-caret-down js-dropnav-toggle"></i>
I/System.out( 7052):             
I/System.out( 7052): <ul class="dropnav-menu dropdown-menu dropnav-menu--has-sidePanel dropnav-menu--has-sidePanel--right dropnav-menu--games arrow--top dropnav-menu--has-borderRadius js-dropnav-active">
I/System.out( 7052):   <li class="dropnav-menu__item dropnav-menu__active">
I/System.out( 7052):     <a href="/new-games/">New Games</a>
I/System.out( 7052):     <ul class="dropnav-submenu">
I/System.out( 7052):       <li class="dropnav-submenu__item dropnav-submenu__viewAll">

【问题讨论】:

    标签: android http http-post


    【解决方案1】:

    我用 Chrome PostMan Rest Client 做了同样的事情,得到了同样的结果。

    我怀疑 Gamespot 是否希望人们像这样抓取他们的页面,也许他们正在通过用户代理标题或类似的东西过滤掉。

    看他们的 HTML,反正我也不想尝试。

    【讨论】:

    • 这是我第一次做任何与 HTTP 相关的事情。网站防止抓取是否常见?感谢这个工具,它看起来真的很酷。
    【解决方案2】:

    可以通过 Java http post 打印 PHP 回显消息

    HttpClient client = new DefaultHttpClient();
    HttpPost postMethod = new HttpPost("http://192.195.68.83/Upload/index.php");
    
    ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
    arrayList.add(new BasicNameValuePair("cmd", "1"));
    arrayList.add(new BasicNameValuePair("comment", "This is comment"));
    
    postMethod.setEntity(new UrlEncodedFormEntity(arrayList));
    
    HttpResponse response = client.execute(postMethod);
    HttpEntity httpEntity = response.getEntity();
    String state = EntityUtils.toString(httpEntity);
    Log.i("chauster",state);
    

    【讨论】:

    • 我不确定这与我的代码有什么不同,虽然它使用了一些不同的东西,但看起来我的代码应该和它一样工作?