use of org.apache.http.ProtocolException in project ABPlayer by winkstu.
the class HttpUtil method httpGetCookie.
public static String httpGetCookie(String url) {
System.out.println("httpGetCookie" + url);
HttpGet httpget = new HttpGet(hostBase + url);
String strResult = "";
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
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;
}
});
httpget.setHeader("Cookie", cookieName + "=" + cookieValue);
HttpResponse response = httpClient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
} else if (response.getStatusLine().getStatusCode() == 302) {
// cookieʧЧ�������ض����־�������µ�¼��ȡ
strResult = "302";
} else if (response.getStatusLine().getStatusCode() == 404) {
strResult = "-1";
}
} catch (Exception e) {
e.printStackTrace();
strResult = "4";
}
return strResult;
}
use of org.apache.http.ProtocolException in project ABPlayer by winkstu.
the class HttpUtil method httpPostCookie.
public static String httpPostCookie(String url, String id, String data) {
System.out.println("httpPostCookie" + url);
String result = "4";
HttpPost httpPost = new HttpPost(hostBase + url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("marc_no", id));
nvps.add(new BasicNameValuePair("r_content", data));
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.setHeader("Cookie", cookieName + "=" + cookieValue);
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
if (response.getStatusLine().getStatusCode() == 200) {
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 result;
}
use of org.apache.http.ProtocolException in project robovm by robovm.
the class DefaultRequestDirector method rewriteRequestURI.
protected void rewriteRequestURI(final RequestWrapper request, final HttpRoute route) throws ProtocolException {
try {
URI uri = request.getURI();
if (route.getProxyHost() != null && !route.isTunnelled()) {
// Make sure the request URI is absolute
if (!uri.isAbsolute()) {
HttpHost target = route.getTargetHost();
uri = URIUtils.rewriteURI(uri, target);
request.setURI(uri);
}
} else {
// Make sure the request URI is relative
if (uri.isAbsolute()) {
uri = URIUtils.rewriteURI(uri, null);
request.setURI(uri);
}
}
} catch (URISyntaxException ex) {
throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex);
}
}
use of org.apache.http.ProtocolException in project robovm by robovm.
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 robovm by robovm.
the class RequestContent 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 (request instanceof HttpEntityEnclosingRequest) {
if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity == null) {
request.addHeader(HTTP.CONTENT_LEN, "0");
return;
}
// Must specify a transfer encoding or a content length
if (entity.isChunked() || entity.getContentLength() < 0) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
}
request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else {
request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE)) {
request.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
request.addHeader(entity.getContentEncoding());
}
}
}
Aggregations