Tag: httpurlconnection

org.apache.http.entity.FileEntity在Android 6(棉花糖)

我将应用程序升级到API 23,其中org.apache.http已弃用。 我目前(不赞成)的代码如下所示: HttpClient httpClient = new DefaultHttpClient(); File file = new File(attr.Value); String url = server_url; HttpPost request = new HttpPost(url); FileEntity fileEntity = new FileEntity(file, "image/png"); request.setEntity(fileEntity); HttpResponse response = httpClient.execute(request); String output = getContent(response.getEntity().getContent()); 我已经find了一些如何使用HttpURLConnection完成这些build议的build议,但是它们HttpURLConnection当前的解决scheme(不能再使用)要复杂得多。 我正在谈论执行与上面相同的function的许多代码行。 例子是: 这个页面和这个页面 有没有人有一个很好的固体更短的解决scheme呢?

如何使用POST将参数添加到HttpURLConnection

我正在尝试使用HttpURLConnection POST (我需要这样使用它,不能使用HttpPost ),我想要添加参数,如连接 post.setEntity(new UrlEncodedFormEntity(nvp)); 哪里 nvp = new ArrayList<NameValuePair>(); 有一些数据存储在我找不到方法如何将此ArrayList添加到我的HttpURLConnection这是: HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); http = https; http.setRequestMethod("POST"); http.setDoInput(true); http.setDoOutput(true); 尴尬的https和http组合的原因是需要不validation证书。 这不是一个问题,但是,它很好地张贴服务器。 但我需要它与论点发表。 有任何想法吗?

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

我成功地使用这个代码通过GET方法发送一些参数的HTTP请求 function void sendRequest(String request) { // ie: 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(); } 现在我可能需要通过POST方法发送参数(即param1,param2,param3),因为它们很长。 我想为该方法添加一个额外的参数(即stringhttpMethod)。 我怎样才能改变上面的代码尽可能less能够通过GET或POST发送参数? 我希望改变 connection.setRequestMethod("GET"); 至 connection.setRequestMethod("POST"); 会做的伎俩,但参数仍然通过GET方法发送。 HttpURLConnection有任何方法可以帮助吗? 有没有什么有用的Java构造? 任何帮助将非常感激。

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

在这里经常会问到java.net.URLConnection用法, Oracle教程 太简单了。 该教程基本上只显示如何触发GET请求并阅读响应。 它没有解释如何使用它来执行POST请求,设置请求头,读取响应头,处理cookie,提交HTML表单,上传文件等。 那么,如何使用java.net.URLConnection来触发和处理“高级”HTTP请求呢?