gpt4 book ai didi

java - HttpResponse 客户端协议(protocol)异常

转载 作者:行者123 更新时间:2023-12-01 12:40:21 25 4
gpt4 key购买 nike

我有一个 HttpGet,一直工作得很好。最近,我获取信息的网页发生了变化。我基本上是从喊播服务器的歌曲元数据页面读取文本。此新 URL 将不允许 HttpResponse response = client.execute(method) 在此新 URL 上运行。它立即抛出客户端协议(protocol)异常,没有进一步的信息,所以我不知道出了什么问题。我下载了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");
}

}

最佳答案

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

所以我添加了:

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

现在我收到了 200 OK 响应。

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

关于java - HttpResponse 客户端协议(protocol)异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25165524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com