在Java中,如果我想在服务器上发送数据到表单,其中表单类型为:
<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方法,因为表单接受方法“post”,因为它在表单定义(初始化)中声明:
<form method="**post**" id="form1" name="form1" action="">
在我的示例(android解决方案)中,我正在使用
__VIEWSTATE
__EVENTTARGET
__EVENTARGUMENT
ctl00$tbUsername
ctl00$tbPwd
ctl00$chkRememberLogin
ctl00$cmdLogin
值,因为它们是服务器发布帖子所需的一次。在没有编程服务器的情况下,我在哪里可以找到服务器所需的内容?我使用WireShark软件查看客户端和服务器之间的所有响应或传出请求,只需使用 http过滤器来仅查看http事务。然后使用任何浏览器以通常的方式在线登录,然后在WireShark中,您将看到浏览器和服务器之间的所有请求和响应。通过已知的IP地址或主机地址找到您感兴趣的那个,然后如果在任何事务上单击右键,则复制可读字节。因此,当您这样做时,您将发现您对服务器的请求必须如何以及需要哪些值。 回到编码(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。
您可以使用
添加标题httppost.addHeader("Referer",
"http://website/login.asp");
或请求的超时值
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
int timeoutConnection = 5000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
在这种情况下,最好在发生超时时捕获异常并再次发出请求,因为建议在HttpClient文档中进行。
默认情况下,HttpClient遵循重定向,但每次使用重定向时都可以捕获:
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;
}
}
}
然后在完成客户端所需的一切后,您可能会释放客户端连接:
public void shutDownClient() {
client.getConnectionManager().shutdown();
}
这是 HttpClient 的示例,请不要忘记您也可以使用 UrlConnection ,这里显示的是差异:
http://www.innovation.ch/java/HTTPClient/urlcon_vs_httpclient.html
因此,这取决于您喜欢使用什么以及哪种更适合您的项目。
P.S。此 POST 解决方案已应用于我的项目,但它可能对您无效。请先使用Wireshark,然后查看必须将哪种请求发送到服务器。就我而言,它就像viewstate = sdsgdgdfd323&amp; username = dsfngkjfdg&amp; password = dsfsdfsfs .....但我知道可能还有其他人。
答案 0 :(得分:0)
URL是否接受PUT代替POST取决于服务器上的完全。
大多数期望POST的服务器都只接受POST。很少使用PUT。