JAVA:http发布请求

我必须向Web服务发送请求,以使用用户名和密码对用户进行身份验证.

我对以下帖子请求有疑问:

public String postTest(String action,ConnectionParametrData [] parameters) {
        Uri.Builder builder = new Uri.Builder().scheme(scheme).authority(authority).path(action);
        uri = builder.build();
        BufferedReader in = null;
        String ans = null;
        HttpPost request = new HttpPost(uri.toString());
        HttpClient defaultClient = new DefaultHttpClient();
        try {
            request.setHeader("Content-Type","application/x-www-form-urlencoded");
            request.setEntity(new UrlEncodedFormEntity(getValuePairs(parameters)));
            HttpResponse response = defaultClient.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"),8192);
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String newLine = System.getProperty("line.separator");
            while((line = in.readLine()) != null) {
                sb.append(line + newLine);
            }
            ans = sb.toString();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        return ans;
    }

当我执行此方法时,服务器抛出错误,告知请求不是发布请求.

但这种方法非常有效:

private String makePost(String action,ConnectionParametrData [] parameters) throws IOException {
        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(scheme).append("://www.").append(authority).append(action);
        URL url = new URL(urlBuild.toString());
        URLConnection urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

        DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream());

        String content = getParameters(parameters);
        printout.writeBytes(content);
        printout.flush();
        printout.close();

        BufferedReader in = null;
        in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()),8192);
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String newLine = System.getProperty("line.separator");
        while((line = in.readLine()) != null) {
            sb.append(line + newLine);
        }
        in.close();
        return sb.toString();
    }

我更喜欢使用HttpClient而不是URLConecction,
有谁知道为什么第一种方法不被批准为POST?

解决方法

在您的第一个代码段中,我没有看到您为登录名和密码设置任何帖子参数的位置.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair> {
        new BasicNameValuePair("login","myusername"),new BasicNameValuePair("password","somepassword")};
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

你可能想看看这个:http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...