java.net.URLConnection在这里经常询问使用情况,Oracle教程对此非常简洁.
该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.
那么,我如何使用java.net.URLConnection触发和处理"高级"HTTP请求?
在Java中,如何编写HTTP请求消息并将其发送到HTTP WebServer?
我成功地使用此代码HTTP通过GET方法发送 带有一些参数的请求
void sendRequest(String request)
{
// i.e.: request = "http://example.com/index.php?param1=a¶m2=b¶m3=c";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
}
Run Code Online (Sandbox Code Playgroud)
现在我可能需要通过POST方法发送参数(即param1,param2,param3),因为它们非常长.我想在该方法中添加一个额外的参数(即String httpMethod).
如何能够尽可能少地更改上面的代码,以便能够通过GET或发送参数POST?
我希望改变
connection.setRequestMethod("GET");
Run Code Online (Sandbox Code Playgroud)
至
connection.setRequestMethod("POST");
Run Code Online (Sandbox Code Playgroud)
本来可以做到的,但参数仍然是通过GET方法发送的.
有HttpURLConnection任何方法可以帮助吗?有没有有用的Java构造?
任何帮助将非常感谢.
我们假设这个URL ...
http://www.example.com/page.php?id=10
Run Code Online (Sandbox Code Playgroud)
(这里需要在POST请求中发送id)
我想发送id = 10到服务器page.php,它在POST方法中接受它.
我怎样才能从Java中做到这一点?
我试过这个:
URL aaa = new URL("http://www.example.com/page.php");
URLConnection ccc = aaa.openConnection();
Run Code Online (Sandbox Code Playgroud)
但我仍然无法弄清楚如何通过POST发送它
我正在尝试修改一些遗留代码,同时又出现以下类型的错误:
访问限制:由于对所需库的限制,无法访问Headers类型中的方法create(JAXBRIContext,Object)..\jre\lib\rt.jar
对于这些进口声明:
import com.sun.xml.internal.bind.api.JAXBRIContext;
import com.sun.xml.internal.ws.api.message.Header;
import com.sun.xml.internal.ws.api.message.Headers;
import com.sun.xml.internal.ws.developer.WSBindingProvider;
Run Code Online (Sandbox Code Playgroud)
一直在寻找这可能意味着什么,以及如何解决它,但无法找到一个明确的答案.一些帖子似乎暗示我有一些JAR包含了实现类,这些类现在可用作核心java发行版的一部分,但据我所知,我所包含的JAR中没有包含上述类的不同/旧版本.
任何人都能告诉我这个错误是什么以及我如何解决这个问题?
感谢您提前提供的帮助,
奥利
使用Java创建简单HTTP服务器的最简单方法是什么?公共区域是否有任何图书馆可以促进这一点?我只需要响应GET/POST,我不能使用应用程序服务器.
最简单的方法是什么?
我试图在Java中创建一个简单的HttpServer来处理GET请求,但是当我尝试获取请求的GET参数时,我注意到HttpExchange类没有方法.
有没有人知道一种简单的方法来读取GET参数(查询字符串)?
这是我的处理程序的样子:
public class TestHandler{
@Override
public void handle(HttpExchange exc) throws IOxception {
String response = "This is the reponse";
exc.sendResponseHeaders(200, response.length());
// need GET params here
OutputStream os = exc.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
Run Code Online (Sandbox Code Playgroud)
..和主要方法:
public static void main(String[] args) throws Exception{
// create server on port 8000
InetSocketAddress address = new InetSocketAddress(8000);
HttpServer server = new HttpServer.create(address, 0);
// bind handler
server.createContext("/highscore", new TestHandler());
server.setExecutor(null);
server.start();
}
Run Code Online (Sandbox Code Playgroud) 我不熟悉哪里可以获得com.sun.net.httpserver包?我喜欢在Android中使用这个包.
谢谢!
我有一个试用任务,包括仅使用Oracle JRE 1.6标准库编写简单的http服务器.这里只使用Java SE API的Java中的简单HTTP服务器 我发现了以下语句:
Since Java 1.6, there's a builtin HTTP server in Sun Oracle JDK (note: JDK, not JRE).
但我认为所有运行时库都包含在JRE中,JDK是JRE +的一些开发工具.另外,我已经下载了JRE 1.6,发现HttpServer包含在rt.jar文件中.所以,我的问题是:
java ×10
http ×5
post ×3
get ×2
android ×1
html ×1
httprequest ×1
httpserver ×1
query-string ×1