标签: httpurlconnection

如何使用java.net.URLConnection来触发和处理HTTP请求

java.net.URLConnection在这里经常询问使用情况,Oracle教程对此非常简洁.

该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.

那么,我如何使用java.net.URLConnection触发和处理"高级"HTTP请求?

java http urlconnection httprequest httpurlconnection

1903
推荐指数
11
解决办法
100万
查看次数

Java - 通过POST方法轻松发送HTTP参数

我成功地使用此代码HTTP通过GET方法发送 带有一些参数的请求

void sendRequest(String request)
{
    // i.e.: request = "http://example.com/index.php?param1=a&param2=b&param3=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构造?

任何帮助将非常感谢.

java post http httpurlconnection

310
推荐指数
8
解决办法
78万
查看次数

如何使用NameValuePair使用POST向HttpURLConnection添加参数

我试图做POSTHttpURLConnection(我需要使用这种方式,不能使用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.事实上,它并没有解决我的问题.

post android http-post httpurlconnection basicnamevaluepair

252
推荐指数
8
解决办法
41万
查看次数

137
推荐指数
4
解决办法
36万
查看次数

如何在HttpURLConnection中发送PUT,DELETE HTTP请求?

我想知道是否可以将PUT,DELETE请求(实际上)发送java.net.HttpURLConnection到基于HTTP的URL.

我已经阅读了很多文章,描述了如何发送GET,POST,TRACE,OPTIONS请求,但我仍然没有找到任何成功执行PUT和DELETE请求的示例代码.

java put httpurlconnection http-delete

131
推荐指数
4
解决办法
25万
查看次数

你能解释一下HttpURLConnection连接过程吗?

HTTPURLConnection用来连接到Web服务.我知道如何使用,HTTPURLConnection但我想了解它是如何工作的.基本上,我想知道以下内容:

  • 在哪一点上HTTPURLConnection尝试建立与给定URL的连接?
  • 在哪一点上我可以知道我能够成功建立连接?
  • 是建立连接并在一步/方法调用中发送实际请求?它是什么方法?
  • 你能解释的功能getOutputStreamgetInputStream外行的任期?我注意到,当我试图连接到服务器宕机,我得到ExceptiongetOutputStream.这是否意味着HTTPURLConnection我只会在调用时开始建立连接getOutputStream?怎么样getInputStream?因为我只能得到响应getInputStream,那么这是否意味着我还没有发送任何请求getOutputStream但只是建立连接?HttpURLConnection我调用时是否回到服务器请求响应getInputStream
  • 我是否正确地说,openConnection只是创建一个新的连接对象,但尚未建立任何连接?
  • 如何衡量读取开销和连接开销?

java inputstream outputstream urlconnection httpurlconnection

131
推荐指数
2
解决办法
11万
查看次数

连接到需要使用Java进行身份验证的远程URL

如何连接到需要身份验证的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)

java httpurlconnection

119
推荐指数
7
解决办法
23万
查看次数

使用带HttpURLConnection的POST发送文件

由于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.

post android client-server http httpurlconnection

119
推荐指数
4
解决办法
15万
查看次数

POST请求发送json数据java HttpUrlConnection

我开发了一个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)

java post json curl httpurlconnection

97
推荐指数
4
解决办法
30万
查看次数

从HttpURLConnection获取InputStream对象时出现FileNotFoundException

我正在尝试使用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 inputstream filenotfoundexception httpurlconnection

96
推荐指数
5
解决办法
11万
查看次数