其他分享
首页 > 其他分享> > HttpUtils

HttpUtils

作者:互联网

HttpUtils

package com.example.day1031;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Httputils {
    private static final String TAG = "Httputils";
    public static String geturl(String s){
        try {
            URL url = new URL(s);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            Log.i(TAG, "geturl: "+connection.getResponseCode());
            if (connection.getResponseCode() == 200){
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                InputStream inputStream = connection.getInputStream();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = inputStream.read(bytes))!=-1){
                    byteArrayOutputStream.write(bytes,0,len);
                }
                return  byteArrayOutputStream.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {


        }
        return null;
    }
}

标签:java,String,HttpUtils,connection,io,import,net
来源: https://blog.csdn.net/zrx_z/article/details/104781613