发布到Html/PHP表单的Java代码
我正在寻找一些方法/代码,了解如何转到URL,将字符串粘贴/键入这些文本框并点击登录按钮发布到Html/PHP表单的Java代码,java,html,http,post,Java,Html,Http,Post,我正在寻找一些方法/代码,了解如何转到URL,将字符串粘贴/键入这些文本框并点击登录按钮 <center><label>Username: </label><input type="text" name="username"/></center> <center><label>Password: </label><input type="password" name="password"
<center><label>Username: </label><input type="text" name="username"/></center>
<center><label>Password: </label><input type="password" name="password"/><br/></center>
<center><input type="submit" value=" Login "/><br /></center>
</form>
用户名:
密码:
我对这一点很陌生,如果你能解释,请解释。
/*
* ====================================================================
*向Apache软件基金会(ASF)授权
*一个或多个参与者许可协议。见通知文件
*与此工作一起分发以获取更多信息
*关于版权所有权。ASF许可此文件
*根据Apache许可证,版本2.0(
*“许可证”);除非符合规定,否则您不得使用此文件
*带着执照。您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,
*根据许可证分发的软件在
*“按原样”的基础上,没有任何
*种类,无论是明示的还是暗示的。请参阅许可证以获取详细信息
*管理权限和限制的特定语言
*根据许可证。
* ====================================================================
*
*该软件由许多人的自愿捐款组成
*代表Apache软件基金会的个人。更多
*关于Apache软件基金会的信息,请参阅
* .
*
*/
包org.apache.http.examples;
导入java.io.ByteArrayInputStream;
导入java.net.Socket;
导入org.apache.http.connectionReuseStragey;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpHost;
导入org.apache.http.HttpRequestInterceptor;
导入org.apache.http.HttpResponse;
导入org.apache.http.HttpVersion;
导入org.apache.http.entity.ByteArrayEntity;
导入org.apache.http.entity.InputStreamEntity;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.defaultConnectionReuseStragey;
导入org.apache.http.impl.DefaultHttpClientConnection;
导入org.apache.http.message.BasicHttpEntityEnclosingRequest;
导入org.apache.http.params.HttpParams;
导入org.apache.http.params.HttpProtocolParams;
导入org.apache.http.params.SyncBasicHttpParams;
导入org.apache.http.protocol.HttpContext;
导入org.apache.http.protocol.BasicHttpContext;
导入org.apache.http.protocol.ExecutionContext;
导入org.apache.http.protocol.HttpProcessor;
导入org.apache.http.protocol.HttpRequestExecutor;
导入org.apache.http.protocol.ImmutableHttpProcessor;
导入org.apache.http.protocol.RequestConControl;
导入org.apache.http.protocol.RequestContent;
导入org.apache.http.protocol.RequestExpectContinue;
导入org.apache.http.protocol.RequestTargetHost;
导入org.apache.http.protocol.RequestUserAgent;
导入org.apache.http.util.EntityUtils;
/**
*按顺序执行多个POST请求的基本示例。
*
*请注意,此应用程序的目的是演示HttpCore API的使用。
*它并不打算演示构建HTTP客户端的最有效方法。
*/
公共类ElementalHttpPost{
公共静态void main(字符串[]args)引发异常{
HttpParams params=新的SyncBasicHttpParams();
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(参数,“UTF-8”);
HttpProtocolParams.setUserAgent(参数,“测试/1.1”);
HttpProtocolParams.setUseExpectContinue(params,true);
HttpProcessor httpproc=新的不可变HttpProcessor(新的HttpRequestInterceptor[]{
//所需的协议拦截器
新建RequestContent(),
新建RequestTargetHost(),
//推荐的协议拦截器
新建RequestConnControl(),
新建RequestUserAgent(),
新的RequestExpectContinue()});
HttpRequestExecutor httpexecutor=新的HttpRequestExecutor();
HttpContext=新的BasicHttpContext(null);
HttpHost主机=新的HttpHost(“本地主机”,8080);
DefaultHttpClientConnection conn=新的DefaultHttpClientConnection();
ConnectionReuseStragey connStrategy=新的默认ConnectionReuseStragey();
setAttribute(ExecutionContext.HTTP_连接,conn);
setAttribute(ExecutionContext.HTTP_TARGET_HOST,HOST);
试一试{
HttpEntity[]请求主体={
新StringEntity(
“这是第一个测试请求”,“UTF-8”),
新ByteArrayEntity(
“这是第二个测试请求”。getBytes(“UTF-8”),
新输入流(
新ByteArrayInputStream(
“这是第三个测试请求(将分块)”
.getBytes(“UTF-8”),-1)
};
for(int i=0;i>请求URI:”+Request.getRequestLine().getUri());
请求。setParams(params);
预处理(请求、httpproc、上下文);
HttpResponse response=httpexecutor.execute(请求、连接、上下文);
响应。设置参数(参数);
后处理(响应、httpproc、上下文);
System.out.println(“?@bmorris591:看起来很有趣,很有希望,谢谢!我喜欢这个网站的另一个原因。
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.examples;
import java.io.ByteArrayInputStream;
import java.net.Socket;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
/**
* Elemental example for executing multiple POST requests sequentially.
* <p>
* Please note the purpose of this application is demonstrate the usage of HttpCore APIs.
* It is NOT intended to demonstrate the most efficient way of building an HTTP client.
*/
public class ElementalHttpPost {
public static void main(String[] args) throws Exception {
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Test/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("localhost", 8080);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
HttpEntity[] requestBodies = {
new StringEntity(
"This is the first test request", "UTF-8"),
new ByteArrayEntity(
"This is the second test request".getBytes("UTF-8")),
new InputStreamEntity(
new ByteArrayInputStream(
"This is the third test request (will be chunked)"
.getBytes("UTF-8")), -1)
};
for (int i = 0; i < requestBodies.length; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
"/servlets-examples/servlet/RequestInfoExample");
request.setEntity(requestBodies[i]);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
System.out.println("<< Response: " + response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
}
} finally {
conn.close();
}
}
}