首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HttpResponse ClientProtocolExecption

HttpResponse ClientProtocolExecption
EN

Stack Overflow用户
提问于 2014-08-07 00:31:27
回答 1查看 62关注 0票数 0

我有一台HttpGet,它一直工作得很好。最近,网页改变了我获取信息的位置。我基本上是从shoutcast服务器上读取歌曲元数据页面的文本。这个新的url不允许在这个新的URL上运行HttpResponse response =client.execute(方法)。它立即抛出客户端协议异常,没有进一步的信息,所以我不知道哪里出了问题。我下载了wireshark。这是响应:HTTP/1.0200OK。旧的url响应是HTTP/1.1 200 OK。我已经用谷歌搜索了几个小时,但还是找不到任何帮助。

有人对此有什么建议或帮助吗?

代码如下:

代码语言:javascript
运行
复制
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");
}

}

EN

回答 1

Stack Overflow用户

发布于 2014-08-07 04:51:38

好的,在WinAmp论坛的帮助下,我发现这个url需要通过用户代理"Mozilla“来请求。

所以我补充道:

代码语言:javascript
运行
复制
method.setHeader("User-Agent","Mozilla");

现在我得到了一个200 OK的响应。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25165524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档