JAVA: http post request

馋奶兔 提交于 2019-12-05 05:32:29

In your first code snippet, I am not seeing where you are setting any post parameters for login and password.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair> {
        new BasicNameValuePair("login", "myusername"),
        new BasicNameValuePair("password", "somepassword")};
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

You may want to look at this: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

@James Black

for setting parameter I have a method:

private List<NameValuePair> getValuePairs(ConnectionParametrData [] parameters) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        if(parameters != null) {
            for (ConnectionParametrData parameter : parameters) {
                nameValuePairs.add(new BasicNameValuePair(parameter.getKey(), parameter.getValues()));
            }
        }
        return nameValuePairs;
    }

@Fildor Exception is thrown from server.

public String getPostPage(String postUrl, NameValuePair[] data,
        String cookie)
{
    String html = "";
    PostMethod method = null;
    String contentStr = null;
    try
    {
        method = new PostMethod(postUrl);
        method.addRequestHeader("User-Agent", USER_AGENT);
        method.addRequestHeader("Host", "asqx.moni.gucheng.com");
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        method.addRequestHeader("Referer", "...");
        method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
        method.addRequestHeader("Cookie", cookie);
        method.addRequestHeader("X-MicrosoftAjax", "Delta=true");
        method.addRequestHeader("Pragma", "no-cache");
//            method.addRequestHeader("Accept-Encoding", "gzip, deflate");
        method.addRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
        method.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        method.addRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
        method.setRequestBody(data);
        int statusCode = client.executeMethod(method);

        if(statusCode == HttpStatus.SC_OK)
        {
            InputStream in = method.getResponseBodyAsStream();
            if (in != null) {
                byte[] tmp = new byte[4096];
                int bytesRead = 0;
                ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
                while ((bytesRead = in.read(tmp)) != -1) {
                    buffer.write(tmp, 0, bytesRead);
                }
                byte[] bt = buffer.toByteArray();
                String gbk = new String(bt, "GBK");
                String utf8 = new String(bt, "UTF-8");
                if (gbk.length() < utf8.length()) {
                    bt = null;
                    bt = gbk.getBytes("UTF-8");
                    html = new String(bt, "UTF-8");
                    html = html.replaceFirst(
                                    "[cC][hH][aA][rR][sS][eE][tT]\\s*?=\\s*?([gG][bB]2312|[gG][bB][kK]|[gG][bB]18030)",
                                    "charset=utf-8");
                } else if (gbk.length() > utf8.length()) {
                    html = buffer.toString();
                } else {
                    html = buffer.toString();
                }
                buffer.close();
                contentStr = new String("abc".getBytes(), "UTF-8");
                contentStr = html;

                in.close();
                in = null;
            }
        }
        else
        {
            contentStr = null;
        }
    } catch (Exception e)
    {
        log.error(e);
    } finally
    {
        if (method != null)
            method.releaseConnection();
    }
    return contentStr;
}

I use the mothod to post and get return content. Hope can help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!