Android BufferedInputStream HTTP POST/GET

发布于 2024-11-18 05:17:20 字数 1476 浏览 2 评论 0原文

我使用 BufferedInputStream 进行 HTTP POST/GET

但出现以下错误

  1. java.io.FileNotFoundException: http://XX.XX.XX.XX/WebWS/data.aspx
  2. 传输端点未连接

为什么会出现此错误。我的代码如下,

URL url = new URL(glob.postUrl);

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

    try {

        httpConn.setDoInput(true);
        httpConn.setDoOutput(true);
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        httpConn.setRequestProperty("Content-Language", "TR");      
        httpConn.setConnectTimeout(12000);


        Iterator<String> reqProps = hMap.keySet().iterator();
        while (reqProps.hasNext()) {
            String key = reqProps.next();
            String value = hMap.get(key);
            httpConn.addRequestProperty(key, value);
        }

        InputStream in = new BufferedInputStream(httpConn.getInputStream());

        StringBuilder builder = new StringBuilder();
        String line;
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } finally {
            in.close();
        }
        httpConn.disconnect();

谢谢。

I Use BufferedInputStream For HTTP POST/GET

But I Get Some Error the Below

  1. java.io.FileNotFoundException: http://XX.XX.XX.XX/WebWS/data.aspx
  2. Transport endpoint is not connected

Why Get This Error. My Code is Below

URL url = new URL(glob.postUrl);

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

    try {

        httpConn.setDoInput(true);
        httpConn.setDoOutput(true);
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        httpConn.setRequestProperty("Content-Language", "TR");      
        httpConn.setConnectTimeout(12000);


        Iterator<String> reqProps = hMap.keySet().iterator();
        while (reqProps.hasNext()) {
            String key = reqProps.next();
            String value = hMap.get(key);
            httpConn.addRequestProperty(key, value);
        }

        InputStream in = new BufferedInputStream(httpConn.getInputStream());

        StringBuilder builder = new StringBuilder();
        String line;
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } finally {
            in.close();
        }
        httpConn.disconnect();

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-11-25 05:17:20

您是否有任何原因不使用 HttpClient

您可以将代码替换为以下内容:

HttpContext httpContext = new BasicHttpContext();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, httpContext);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String page = EntityUtils.toString(entity);

您可以使用 ClientConnectionManager 和 HttpParams 设置 HttpClient 以确保安全性,并在初始化时为客户端设置各种 http 参数(如果您搜索类名,则会有大量示例)。

Is there any reason you're not using HttpClient?

You can replace your code with something like:

HttpContext httpContext = new BasicHttpContext();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, httpContext);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String page = EntityUtils.toString(entity);

You can setup the HttpClient with ClientConnectionManager and HttpParams for security and various http parameters for the client at initialisation (plenty of examples around if you search on class names).

池予 2024-11-25 05:17:20

如果 HTTP 响应状态代码为 400 或更高(即服务器端出现任何错误情况),HttpURLConnectionImpl.getInputStream() 会抛出 FileNotFoundException。您应该检查状态代码到底是什么,以获得合适的调试信息。

不过,我赞同 Mark Fisher 关于使用 HttpClient 的建议,据我所知,这是在 Android 上使用 HTTP 的首选方式。

HttpURLConnectionImpl.getInputStream() is known to throw a FileNotFoundException if the HTTP response status code is 400 or higher, i.e. for any error condition on the server side. You should check what the status code really is in order to obtain suitable debug information.

However, I second Mark Fisher's suggestion about using HttpClient, which AFAIK is the preferred way of working with HTTP on Android.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文