java.net.URLConnection
在这里经常询问使用情况,Oracle教程对此非常简洁.
该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.
那么,我如何使用java.net.URLConnection
触发和处理"高级"HTTP请求?
我成功地使用此代码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构造?
任何帮助将非常感谢.
我试图做POST与HttpURLConnection
(我需要使用这种方式,不能使用HttpPost
),我想参数添加到连接如
post.setEntity(new UrlEncodedFormEntity(nvp));
Run Code Online (Sandbox Code Playgroud)
哪里
nvp = new ArrayList<NameValuePair>();
Run Code Online (Sandbox Code Playgroud)
有一些数据存储在.我找不到如何将此添加ArrayList
到我HttpURLConnection
这里的方法:
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
Run Code Online (Sandbox Code Playgroud)
这种尴尬的https和http组合的原因是不需要验证证书.但这不是问题,它可以很好地发布服务器.但是我需要用论据发帖.
有任何想法吗?
重复免责声明:
回到2012年,我不知道如何将参数插入到HTTP POST请求中.我一直在坚持,NameValuePair
因为它是在教程中.这个问题可能看似重复,但是,我的2012年自我阅读了其他问题并且它没有使用NameValuePair
.事实上,它并没有解决我的问题.
我想知道是否可以将PUT,DELETE请求(实际上)发送java.net.HttpURLConnection
到基于HTTP的URL.
我已经阅读了很多文章,描述了如何发送GET,POST,TRACE,OPTIONS请求,但我仍然没有找到任何成功执行PUT和DELETE请求的示例代码.
我HTTPURLConnection
用来连接到Web服务.我知道如何使用,HTTPURLConnection
但我想了解它是如何工作的.基本上,我想知道以下内容:
HTTPURLConnection
尝试建立与给定URL的连接?getOutputStream
和getInputStream
外行的任期?我注意到,当我试图连接到服务器宕机,我得到Exception
的getOutputStream
.这是否意味着HTTPURLConnection
我只会在调用时开始建立连接getOutputStream
?怎么样getInputStream
?因为我只能得到响应getInputStream
,那么这是否意味着我还没有发送任何请求getOutputStream
但只是建立连接?HttpURLConnection
我调用时是否回到服务器请求响应getInputStream
?openConnection
只是创建一个新的连接对象,但尚未建立任何连接?java inputstream outputstream urlconnection httpurlconnection
如何连接到需要身份验证的Java远程URL.我试图找到一种方法来修改以下代码,以便能够以编程方式提供用户名/密码,因此它不会抛出401.
URL url = new URL(String.format("http://%s/manager/list", _host + ":8080"));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
Run Code Online (Sandbox Code Playgroud) 由于Android开发人员建议使用HttpURLConnection
该类,我想知道是否有人可以为我提供一个很好的示例,说明如何通过POST将位图"文件"(实际上是内存中的流)发送到Apache HTTP服务器.我对cookie或身份验证或任何复杂的东西不感兴趣,但我只想拥有一个可靠的逻辑实现.我在这里看到的所有例子看起来更像是"让我们试试这个,也许它有效".
现在,我有这个代码:
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://example.com/server.cgi");
urlConnection = (HttpURLConnection) url.openConnection();
} catch (Exception e) {
this.showDialog(getApplicationContext(), e.getMessage());
}
finally {
if (urlConnection != null)
{
urlConnection.disconnect();
}
}
Run Code Online (Sandbox Code Playgroud)
showDialog应该只显示一个AlertDialog
(如果URL无效?).
现在,让我们说我生成一个这样的位图:Bitmap image = this.getBitmap()
在一个派生的控件中View
,我想通过POST发送它.实现这样的事情的正确程序是什么?我需要使用哪些课程?我可以HttpPost
在这个例子中使用吗?如果是这样,我将如何构建InputStreamEntity
我的位图?我觉得要首先将位图存储在设备上的文件中是令人反感的.
我还要提一下,我真的需要将原始位图的每个未经改变的像素发送到服务器,因此我无法将其转换为JPEG.
我开发了一个java代码,使用URL和HttpUrlConnection将以下cURL转换为java代码.卷曲是:
curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}'
Run Code Online (Sandbox Code Playgroud)
我编写了这段代码,但它始终给出了HTTP代码400错误的请求.我找不到遗漏的东西.
String url="http://url.com";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
JSONObject cred = new JSONObject();
JSONObject auth = new JSONObject();
JSONObject parent = new JSONObject();
cred.put("username","adm");
cred.put("password", "pwd");
auth.put("tenantName", "adm");
auth.put("passwordCredentials", cred.toString());
parent.put("auth", auth.toString());
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());
wr.flush();
//display what returns the POST request
StringBuilder sb = new StringBuilder(); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HttpURLConnection向网址发送帖子请求(在java中使用cUrl).请求的内容是xml,在结束时,应用程序处理xml并将记录存储到数据库,然后以xml字符串的形式发回响应.该应用程序在本地托管在apache-tomcat上.
当我从终端执行此代码时,会按预期将一行添加到数据库中.但是从连接获取InputStream时会抛出异常,如下所示
java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30)
Run Code Online (Sandbox Code Playgroud)
这是代码
public class HttpCurl {
public static void main(String [] args) {
HttpURLConnection con;
try {
con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
File xmlFile = new File("test.xml");
String xml = ReadWriteTextFile.getContents(xmlFile);
con.getOutputStream().write(xml.getBytes("UTF-8"));
InputStream response = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
for (String line ; (line = reader.readLine()) != null;) {
System.out.println(line);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException …
Run Code Online (Sandbox Code Playgroud) java ×8
http ×4
post ×4
android ×2
inputstream ×2
curl ×1
get ×1
http-delete ×1
http-post ×1
httprequest ×1
json ×1
outputstream ×1
put ×1