Java的httpPost成.asp的形式 [英] Java httpPost into .asp form

查看:122
本文介绍了Java的httpPost成.asp的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如果我要发送数据形成服务器,其中表单类型是:

In Java, if I want to send data to form on the server, where form type is:

 <form method="post" id="form1" name="form1" action="">
 <div id="login" class="box">
  <div class="header">Log in</div>
  <div class="content">
   <label for="txtUser">User:</label>
   <input id="txtUser" name="txtUser" type="text" size="13" value="" />
   <label for="txtPassword">Password:</label>
   <input id="txtPassword" name="txtPassword" type="password" size="13" value="" />
   <input id="BLogin" name="BLogin" type="submit" value="Log in"  />
  </div>
  <div class="footer">
   <input type="checkbox" id="chkSave" name="chkSave"   /> <label for="chkSave">Save account</label>
  </div>
 </div>
 </form>

在这种情况下,我必须使用HttpPost方法,因为形式接受的方法后,因为它是在表单定义(初始化)说:

in this case I must use HttpPost method, since the form accepts the method "post" as it is stated in the form definition(initialization):

<form method="**post**" id="form1" name="form1" action="">

在我的例子(Android解决方案),我现在用的是

In my example(android solution) i am using the

__VIEWSTATE
__EVENTTARGET
__EVENTARGUMENT
ctl00$tbUsername
ctl00$tbPwd
ctl00$chkRememberLogin
ctl00$cmdLogin

的值,因为它们是所要求的服务器以使柱的一次。在哪里可以找到所需要通过的时候,你有没有设定服务器的情况下,服务器?我使用的是Wireshark软件查看所有进来的响应或客户端和服务器之间的传出请求,只需使用 HTTP过滤器只看到HTTP事务。然后,使用任何浏览器作为你这样做在线,然后在Wireshark中,您将看到您的浏览器和服务器之间的所有请求和响应通常的方式登录。找一个你是通过已知的IP地址或主机地址感兴趣,然后复制可读字节,你觉得如果你点击任何交易的右键。所以,当你这样做,你会发现你的请求到服务器,怎么要像,哪些是需要的值。 返回编码(JAVA):

values, since they are the once that are required by the server to make the post. Where do I find what is required by the server in the case when you have not programmed the server? I am using the WireShark software to see all the incomming responses or outgoing requests between the client and the server, just use the http filter to see only the http transactions. Then use any browser to login in the usual way as you do it online and then in WireShark you will see all the requests and responses between your browser and server. Find the one you are interested in by the known IP address or the host address and then copy the readable bytes which you find if you click the right button on any of the transactions. So when you do that, you will find how your request to the server must look like and which values are needed. Back to coding(java):

public HttpResponse httpPost1(String viewstateValue, String url, String username, String password)
            throws ConnectTimeoutException {
            try {
                // --------post
                HttpPost httppost = new HttpPost(url);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE",
                        viewstateValue));
                nameValuePairs.add(new BasicNameValuePair("__EVENTTARGET", ""));
                nameValuePairs
                        .add(new BasicNameValuePair("__EVENTARGUMENT", ""));

                nameValuePairs.add(new BasicNameValuePair("ctl00$tbUsername",
                        username));
                nameValuePairs.add(new BasicNameValuePair("ctl00$tbPwd", password));
                nameValuePairs.add(new BasicNameValuePair(
                        "ctl00$chkRememberLogin", "0"));
                nameValuePairs.add(new BasicNameValuePair("ctl00$cmdLogin",
                        "Login"));
                // nameValuePairs.add(new
                // BasicNameValuePair("ctl00$cmdForgetMe",
                // "Forget Me"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = client.execute(httppost);

                String responseHtml = EntityUtils.toString(response
                        .getEntity());
                // System.out.println(responseHtml);
                // System.out.println(response1.getStatusLine());

            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }

        return response;
    }

下面我发布的价值观,而我事先知道的URL。

Here i post the values to the URL which i know in advance.

您可以添加使用标题

httppost.addHeader("Referer",
                    "http://website/login.asp");

或超时值的请求

or time-out value to the request

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            int timeoutConnection = 5000;
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);

在这种情况下,最好是再次捕获异常,如果发生超时,并发出请求,因为它是宜做HttpClient的文档中。

In this case it is better to catch the exceptions if the timeout occured and make the request again, as it is advised to do in the HttpClient documentation.

的HttpClient如下重定向默认,但有可能将发生重定向时通过使用以每次捉:

HttpClient follows redirects by default, but it is possible to catch everytime the redirect occurs by using:

private RedirectHandler customRedirectHandler;

........
(maybe constructor..)
    client.setRedirectHandler(customRedirectHandler);
........
class CustomRedirectHandler extends DefaultRedirectHandler {
        @Override
        public boolean isRedirectRequested(HttpResponse response,
                HttpContext context) {
            // System.out.println("isRedirectRequested");

            return true;
        }

        @Override
        public URI getLocationURI(HttpResponse response, HttpContext context)
                throws ProtocolException {

            String location = response.getLastHeader("Location").getValue();

            if (location.contains("Login")) {
                // System.out.println("Login needed");
            } else {
                // System.out.println("No login required");
            }

            URI redirectURI = null;
            try {
                redirectURI = new URI(location);
            } catch (URISyntaxException e) {
            }

            return redirectURI;
        }
    }
}

再经过这样做,你需要与客户端,您可以释放你的客户端的连接一切:

and then after doing everything that you need with the client you might release your clients connection:

public void shutDownClient() {
    client.getConnectionManager().shutdown();
}

这是一个例子的HttpClient ,不要忘了,你也可能会使用的URLConnection ,这里显示的差异:

This is an example of the HttpClient, do not forget that you might also use the UrlConnection, here are differences shown:

http://www.innovation.ch/java/HTTPClient/urlcon_vs_httpclient.html

因此​​,它取决于你preFER使用,什么是更适合您的项目。

So it depends what you prefer to use and what is more appropriate for your project.

P.S。这发表解决方案应用到我的项目,但它可能不适合你。首先使用Wireshark,然后看看有什么样的要求必须发送到服务器的工作。在我的情况下,它就像视图状态= sdsgdgdfd323和放大器;用户名= dsfngkjfdg和放大器;密码= dsfsdfsfs .....但我知道,有可能是其他

P.S. This POST solution is applied to my project, but it may not work for yours.. Use Wireshark first and then see what kind of requests must be sent to the server. In my case it was like viewstate=sdsgdgdfd323&username=dsfngkjfdg&password=dsfsdfsfs..... but i know that there might be others.

推荐答案

无论是否接受URL代替POST的PUT依赖的完全的服务器上。

Whether or not the URL accepts a PUT in place of a POST depends entirely on the server.

这是期望POST将只接受POST大多数服务器。 a将用于更为罕见。

Most servers that expect a POST will accept only a POST. A PUT is used much more rarely.

这篇关于Java的httpPost成.asp的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆