use of org.apache.http.ProtocolException in project platform_external_apache-http by android.
the class RequestTargetHost method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (targethost == null) {
HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}
}
use of org.apache.http.ProtocolException in project platform_external_apache-http by android.
the class DefaultResponseParser method parseHead.
@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
// clear the buffer
this.lineBuf.clear();
//read out the HTTP status string
int count = 0;
ParserCursor cursor = null;
do {
int i = sessionBuffer.readLine(this.lineBuf);
if (i == -1 && count == 0) {
// The server just dropped connection on us
throw new NoHttpResponseException("The target server failed to respond");
}
cursor = new ParserCursor(0, this.lineBuf.length());
if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
// Got one
break;
} else if (i == -1 || count >= this.maxGarbageLines) {
// Giving up
throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
}
count++;
} while (true);
//create the status line from the status string
StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
return this.responseFactory.newHttpResponse(statusline, null);
}
use of org.apache.http.ProtocolException in project ABPlayer by winkstu.
the class HttpUtil method GetCookie.
public static Integer GetCookie(String url, String number, String pw, String select, String host) {
System.out.println("GetCookie");
int result = 4;
HttpPost httpPost = new HttpPost(hostBase + url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("number", number));
nvps.add(new BasicNameValuePair("passwd", pw));
nvps.add(new BasicNameValuePair("select", select));
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
try {
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
return 2;
} else if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length > 0) {
List<Cookie> list = httpClient.getCookieStore().getCookies();
for (Cookie c : list) {
cookieName = c.getName();
cookieValue = c.getValue();
}
System.out.println(cookieName + cookieValue);
return 3;
}
} else if (response.getStatusLine().getStatusCode() == 404) {
return -1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of org.apache.http.ProtocolException in project ABPlayer by winkstu.
the class HttpUtil method getCookie.
public static int getCookie(String url) {
System.out.println("getCookie" + url);
HttpGet httpGet = new HttpGet(hostBase + url);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
HttpResponse response = httpClient.execute(httpGet);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
if (response.getStatusLine().getStatusCode() == 200) {
Header[] heads = response.getAllHeaders();
System.out.println(heads.length);
for (Header header : heads) {
System.out.println(header.getName() + " = " + header.getValue());
}
return 2;
} else if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length > 0) {
System.out.println(headers[0].getValue());
return 3;
}
} else if (response.getStatusLine().getStatusCode() == 404) {
return -1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
use of org.apache.http.ProtocolException in project undertow by undertow-io.
the class SaveOriginalPostRequestTestCase method createHttpClient.
private TestHttpClient createHttpClient() {
TestHttpClient client = new TestHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
return true;
}
return super.isRedirected(request, response, context);
}
});
return client;
}
Aggregations