我有一台HttpGet,它一直工作得很好。最近,网页改变了我获取信息的位置。我基本上是从shoutcast服务器上读取歌曲元数据页面的文本。这个新的url不允许在这个新的URL上运行HttpResponse response =client.execute(方法)。它立即抛出客户端协议异常,没有进一步的信息,所以我不知道哪里出了问题。我下载了wireshark。这是响应:HTTP/1.0200OK。旧的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");
}}
发布于 2014-08-07 04:51:38
好的,在WinAmp论坛的帮助下,我发现这个url需要通过用户代理"Mozilla“来请求。
所以我补充道:
method.setHeader("User-Agent","Mozilla");现在我得到了一个200 OK的响应。
也许这会帮助其他人解决这类问题。
乔
https://stackoverflow.com/questions/25165524
复制相似问题