标签: httpurlconnection

HttpEntity现在在Android上被弃用了,还有什么选择?

随着Android 5.1的发布,看起来所有Apache http的东西都被弃用了.看文档是没用的; 他们都说

This class was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

这是第一次阅读它时没关系,但是当每个被弃用的类都说明了这一点时,它没那么有用.

总之,什么是类的替代喜欢HttpEntity,特别是StringEntityMultipartEntity我替换BasicNameValuePair了我自己的Android Pair<T, S>类实现,它看起来URLEncoder.encode是一个很好的替代品URLEncodedUtils,但我不知道该怎么做HttpEntity.

编辑

我决定重新编写网络内容.要尝试使用Retrofit和OkHttp

编辑

认真看看将你的通话和东西转换为Retrofit.很漂亮.我很高兴我做到了.有一些障碍,但它很酷.

android httpurlconnection apache-httpclient-4.x

45
推荐指数
2
解决办法
5万
查看次数

如何为java HttpURLConnection流量启用线路记录?

我在另一个项目中使用了Jakarta commons HttpClient,我希望使用相同的线路日志输出,但使用"标准"HttpUrlConnection.

我使用Fiddler作为代理,但我想直接从java记录流量.

捕获连接输入和输出流所发生的事情是不够的,因为HTTP标头是由HttpUrlConnection类编写和使用的,因此我将无法记录标头.

java logging http httpurlconnection

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

冰淇淋三明治中HttpURLConnection的FileNotFoundException

我有一个适用于Android 2.x和3.x的Android应用程序,但在Android 4.x上运行时会失败.

问题出在这部分代码中:

URL url = new URL("http://blahblah.blah/somedata.xml");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();

InputStream inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)

当应用程序在Android 4.x上运行时,getInputStream()调用会产生一个FileNotFoundException.当在早期版本的Android上运行相同的二进制文件时,它会成功.URL也适用于Web浏览器和curl.

显然HttpURLConnection,ICS中有些变化.有没有人知道发生了什么变化,和/或修复可能是什么?

android filenotfoundexception httpurlconnection

44
推荐指数
3
解决办法
4万
查看次数

从Android发送JSON HTTP POST请求

我正在使用下面的代码发送一个http POST请求,该请求将对象发送到WCF服务.这工作正常,但如果我的WCF服务还需要其他参数会发生什么?如何从我的Android客户端发送它们?

这是我到目前为止编写的代码:

StringBuilder sb = new StringBuilder();  

String http = "http://android.schoolportal.gr/Service.svc/SaveValues";  


HttpURLConnection urlConnection=null;  
try {  
    URL url = new URL(http);  
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);   
    urlConnection.setRequestMethod("POST");  
    urlConnection.setUseCaches(false);  
    urlConnection.setConnectTimeout(10000);  
    urlConnection.setReadTimeout(10000);  
    urlConnection.setRequestProperty("Content-Type","application/json");   

    urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
    urlConnection.connect();  

    //Create JSONObject here
    JSONObject jsonParam = new JSONObject();
    jsonParam.put("ID", "25");
    jsonParam.put("description", "Real");
    jsonParam.put("enable", "true");
    OutputStreamWriter out = new   OutputStreamWriter(urlConnection.getOutputStream());
    out.write(jsonParam.toString());
    out.close();  

    int HttpResult =urlConnection.getResponseCode();  
    if(HttpResult ==HttpURLConnection.HTTP_OK){  
        BufferedReader br = new BufferedReader(new InputStreamReader(  
            urlConnection.getInputStream(),"utf-8"));  
        String line = null;  
        while ((line = br.readLine()) != null) {  
            sb.append(line + …
Run Code Online (Sandbox Code Playgroud)

java wcf android json httpurlconnection

44
推荐指数
2
解决办法
15万
查看次数

HttpURLConnection java.io.FileNotFoundException

如果我连接到似乎是Apache服务器的代码,下面的代码工作得很好,但是当我尝试连接到我的.Net服务器时,它会抛出错误.我猜这是一个标题要求,但无论我尝试什么,我似乎无法得到成功的回应.

public String Download(String Url)
{
 String filepath=null;
 try {
  //set the download URL, a url that points to a file on the internet
  //this is the file to be downloaded
  URL url = new URL(Url);
  //create the new connection
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

  //set up some things on the connection
  urlConnection.setRequestMethod("GET");
  urlConnection.setDoOutput(true); 
   //and connect!
  urlConnection.connect();
  //set the path where we want to save the file
  //in this case, going to save it on the root directory of the …
Run Code Online (Sandbox Code Playgroud)

android download filenotfoundexception httpurlconnection

44
推荐指数
2
解决办法
7万
查看次数

Android HttpsUrlConnection eofexception

我有一个问题,当我尝试读取任何输入时,我的HttpsURLConnection将抛出一个EOFException.该代码适用于某些网络调用,但在其他网络调用上失败.如果我尝试从连接中读取任何内容,则会因上述错误而失败.

例:

urlConnect.getResponseCode() // will throw error
urlConnect.getResponseMessage() // will throw error
BufferedInputStream in = new BufferedInputStream(urlConnect.getInputStream()); //will throw error
Run Code Online (Sandbox Code Playgroud)

以下是每个的堆栈跟踪:

GETRESPONSE:

03-14 09:49:18.547: W/System.err(6270): java.io.EOFException
03-14 09:49:18.547: W/System.err(6270):     at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
03-14 09:49:18.547: W/System.err(6270):     at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
Run Code Online (Sandbox Code Playgroud)

的BufferedInputStream:

03-14 09:39:14.077: W/System.err(5935): java.io.EOFException
03-14 09:39:14.077: W/System.err(5935):     at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-14 09:39:14.077: W/System.err(5935):     at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
03-14 09:39:14.077: W/System.err(5935):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
03-14 09:39:14.077: W/System.err(5935):     at …
Run Code Online (Sandbox Code Playgroud)

android httpurlconnection eofexception

40
推荐指数
1
解决办法
2万
查看次数

简单的HttpURLConnection POST文件multipart/form-data从android到google blobstore

我不知道html是如何工作的.我想要做的是与以下内容完全类似,但在android上

<body>
    <form action="<%= some_url %>" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
</body>
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码 -

private static void postToUrl(String url_to_upload_on,
        String file_name_with_ext, byte[] byteArray) {

    String attachmentName = "file";
    String attachmentFileName = file_name_with_ext;
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    try{

    URL url = new URL(url_to_upload_on);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    connection.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + boundary);
    DataOutputStream request = new DataOutputStream(
            connection.getOutputStream()); 
    request.writeBytes(twoHyphens + boundary + crlf);
    request.writeBytes("Content-Disposition: form-data; …
Run Code Online (Sandbox Code Playgroud)

android multipartform-data file httpurlconnection blobstore

39
推荐指数
3
解决办法
5万
查看次数

如何在java中发送Https Post请求

我想从java代码登录应用程序.这是我的代码......

String httpsURL = "https://www.abcd.com/auth/login/";

String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8"); 
query += "&";
query += "password="+URLEncoder.encode("abcd","UTF-8") ;

URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");

con.setRequestProperty("Content-length", String.valueOf(query.length())); 
con.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); 
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"); 
con.setDoOutput(true); 
con.setDoInput(true); 

DataOutputStream output = new DataOutputStream(con.getOutputStream());  


output.writeBytes(query);

output.close();

DataInputStream input = new DataInputStream( con.getInputStream() ); 



for( int c = input.read(); c != -1; c = input.read() ) 
System.out.print( (char)c ); 
input.close(); 

System.out.println("Resp Code:"+con .getResponseCode()); 
System.out.println("Resp Message:"+ con .getResponseMessage()); 
Run Code Online (Sandbox Code Playgroud)

但我无法登录,它只返回登录页面.

如果有人可以,请帮助我理解我做错了什么.

java http-post httpurlconnection

36
推荐指数
1
解决办法
9万
查看次数

如何使用cookieManager在httpUrlConnection中处理cookie

我有一个返回多个cookie的服务器请求,如下所示:

在此输入图像描述

这就是我将这些cookie存储到cookieManager的方式:

HttpURLConnection connection = ... ;
static java.net.CookieManager msCookieManager = new java.net.CookieManager();
msCookieManager.put(COOKIES_URI, connection.getHeaderFields());
Run Code Online (Sandbox Code Playgroud)

这就是我将这些cookie添加到下一个连接的方式:

connection.setRequestProperty("Cookie", 
  msCookieManager.getCookieStore().get(COOKIES_URI).toString());
Run Code Online (Sandbox Code Playgroud)

它是从cookieManager获取cookie的正确方法吗?我很确定有一个更好的...

cookies android httpurlconnection android-cookiemanager

36
推荐指数
2
解决办法
8万
查看次数

HttpURLConnection发送带有参数的图像,音频和视频文件可能(String或Json String)Android

我正在分享使用HttpURLConnection发送带参数的图像,音频视频文件的解决方案.参数可以是(纯字符串或JSON).

(Android客户端到PHP后端)

场景:必须上传媒体文件(带参数的音频,视频和图像).

媒体文件将存储在服务器文件夹中,参数将存储到db.

我遇到了一个问题,即在参数丢失图像上传成功.

找到了潜在的解

如此推荐选择HttpURLConnection而不是Httpclient

您可能想知道,哪个客户最好?

Apache HTTP客户端在Eclair和Froyo上的错误更少.它是这些版本的最佳选择.

对于姜饼和更好的,HttpURLConnection是最好的选择.其简单的API和小巧的尺寸使其非常适合Android.透明压缩和响应缓存可减少网络使用,提高速度并节省电池电量.新的应用程序应该使用HttpURLConnection; 这是我们将继续投入精力的地方.

Android代码:

public int uploadFile(final String sourceFileUri) {

    String fileName = sourceFileUri;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new …
Run Code Online (Sandbox Code Playgroud)

android httpurlconnection

36
推荐指数
1
解决办法
2万
查看次数