【问题标题】:HttpResponse ClientProtocolExecptionHttpResponse 客户端协议执行
【发布时间】:2014-08-07 04:51:38
【问题描述】:

我有一个运行良好的 HttpGet。最近,我从哪里获取信息的网页发生了变化。我基本上是从页面上读取来自广播服务器的歌曲元数据的文本。这个新 URL 将不允许 HttpResponse response = client.execute(method) 在这个新 URL 上运行。它立即抛出 clientprotocolexception,没有更多信息,所以我不知道出了什么问题。我下载了wireshark。这是响应:HTTP/1.0 200 OK。旧的 url 响应是 HTTP/1.1 200 OK。我已经用谷歌搜索了几个小时,但找不到任何帮助。

有没有人对此有任何建议或帮助?

代码如下:

package com.joebutt.mouseworldradio;

import java.io.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;


import android.os.AsyncTask;
import android.util.Log;


public class HttpMetaData extends AsyncTask<Void, Void, String>
{

@Override
protected String doInBackground(Void... params)
{
    Log.d("MWR MetaData", "doInBackground called");
    HttpClient client = new DefaultHttpClient();
    HttpGet method = new HttpGet("http://wdwtoday.serverroom.us:4152/7.html");
    //HttpGet method = new HttpGet("http://38.96.148.18:4152/7.html");
    //method.setHeader("HTTP/1.1", "200 OK");
    //method.setHeader("Content-Type:", "text/html");
    //old url that worked just fine for 2 years
    //HttpGet method = new HttpGet("http://yp.shoutcast.com/Metadata_Info1.php?surl=" + Play.selectedUrl);
    String responseData = "";
    if(isCancelled())
    {
        return responseData;
    }
    try
    {
        //fails at the response!!
        HttpResponse response = client.execute(method);
        int status = response.getStatusLine().getStatusCode();
        String statusString = Integer.toString(status);
        Log.d("MWR Http status code", statusString);
        if (status == HttpStatus.SC_OK)
        {
            HttpEntity entity = response.getEntity();
            byte buffer[] = new byte[1024];
            InputStream inStream = entity.getContent();
            int numBytes = inStream.read(buffer);
            inStream.close();
            responseData = new String(buffer,0,numBytes);

            //get rid of the first part of the string
            if(responseData.length() > 13)
            {
                responseData = responseData.substring(13);

                //now get rid of the end of the string to clean it up
                //int length = responseData.length();
                int endPoint = responseData.indexOf("'");
                responseData = responseData.substring(0, endPoint);

                //old stuff wasnt used 8/2014
                //if (Play.selectedUrl.equals("http://38.96.148.91:4152"))
                //{
                    //int trimAmount = length - 37;
                    //responseData = responseData.substring(0, trimAmount);
                //}
                //else if (Play.selectedUrl.equals("http://38.96.148.91:4154"))
                //{
                //  int trimAmount = length - 31;
                    //responseData = responseData.substring(0, trimAmount);
                //}
            }
            else
            {
                responseData = "Data is not currently available";
            }
        }
        else
        {
            responseData = "Data is not currently available";
        }
    }
    catch(ClientProtocolException e)
    {
        Log.d("MWR MetaData", "Response Failure: " + e + "/" + responseData);
        responseData = "Data Error";
    }
    catch(IOException e)
    {
        Log.d("MWR MetaData", "IOException" + e + "/" + responseData);
        responseData = "Data is not currently available";
    }

    return responseData;
}

@Override
protected void onPostExecute(String result)
{

    Play.metaData=result;
    Log.d("MWR getMetaData", "onPostExecute Called");
}

}

【问题讨论】:

    标签: java android


    【解决方案1】:

    好的,在 WinAmp 论坛的帮助下,我发现需要使用用户代理“Mozilla”请求此 URL。

    所以我补充说:

    method.setHeader("User-Agent","Mozilla");
    

    现在我得到了 200 OK 响应。

    也许这会帮助其他人解决此类问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      相关资源
      最近更新 更多