Search in sources :

Example 1 with ParseException

use of org.apache.http.ParseException in project android-app by eoecn.

the class CustomHttpClient method getFromWebByHttpClient.

public static String getFromWebByHttpClient(Context context, String url, NameValuePair... nameValuePairs) throws Exception {
    log.d("getFromWebByHttpClient url = " + url);
    try {
        // http地址
        // String httpUrl =
        // "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (nameValuePairs != null && nameValuePairs.length > 0) {
            sb.append("?");
            for (int i = 0; i < nameValuePairs.length; i++) {
                if (i > 0) {
                    sb.append("&");
                }
                sb.append(String.format("%s=%s", nameValuePairs[i].getName(), nameValuePairs[i].getValue()));
            }
        }
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(sb.toString());
        // 取得HttpClient对象
        HttpClient httpclient = getHttpClient(context);
        // 请求HttpClient,取得HttpResponse
        HttpResponse httpResponse = httpclient.execute(httpRequest);
        // 请求成功
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new RuntimeException(context.getResources().getString(R.string.httpError));
        }
        return EntityUtils.toString(httpResponse.getEntity());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.e("IOException ");
        e.printStackTrace();
        throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 2 with ParseException

use of org.apache.http.ParseException in project robovm by robovm.

the class AbstractMessageParser method parse.

public HttpMessage parse() throws IOException, HttpException {
    HttpMessage message = null;
    try {
        message = parseHead(this.sessionBuffer);
    } catch (ParseException px) {
        throw new ProtocolException(px.getMessage(), px);
    }
    Header[] headers = AbstractMessageParser.parseHeaders(this.sessionBuffer, this.maxHeaderCount, this.maxLineLen, this.lineParser);
    message.setHeaders(headers);
    return message;
}
Also used : ProtocolException(org.apache.http.ProtocolException) Header(org.apache.http.Header) ParseException(org.apache.http.ParseException) HttpMessage(org.apache.http.HttpMessage)

Example 3 with ParseException

use of org.apache.http.ParseException in project robovm by robovm.

the class BasicLineParser method parseStatusLine.

// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        // handle the HTTP-Version
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        // handle the Status-Code
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            blank = indexTo;
        }
        int statusCode = 0;
        try {
            statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
        } catch (NumberFormatException e) {
            throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
        }
        //handle the Reason-Phrase
        i = blank;
        String reasonPhrase = null;
        if (i < indexTo) {
            reasonPhrase = buffer.substringTrimmed(i, indexTo);
        } else {
            reasonPhrase = "";
        }
        return createStatusLine(ver, statusCode, reasonPhrase);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 4 with ParseException

use of org.apache.http.ParseException in project robovm by robovm.

the class BasicLineParser method parseProtocolVersion.

// non-javadoc, see interface LineParser
public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    final String protoname = this.protocol.getProtocol();
    final int protolength = protoname.length();
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    skipWhitespace(buffer, cursor);
    int i = cursor.getPos();
    // long enough for "HTTP/1.1"?
    if (i + protolength + 4 > indexTo) {
        throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
    }
    // check the protocol name and slash
    boolean ok = true;
    for (int j = 0; ok && (j < protolength); j++) {
        ok = (buffer.charAt(i + j) == protoname.charAt(j));
    }
    if (ok) {
        ok = (buffer.charAt(i + protolength) == '/');
    }
    if (!ok) {
        throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
    }
    i += protolength + 1;
    int period = buffer.indexOf('.', i, indexTo);
    if (period == -1) {
        throw new ParseException("Invalid protocol version number: " + buffer.substring(indexFrom, indexTo));
    }
    int major;
    try {
        major = Integer.parseInt(buffer.substringTrimmed(i, period));
    } catch (NumberFormatException e) {
        throw new ParseException("Invalid protocol major version number: " + buffer.substring(indexFrom, indexTo));
    }
    i = period + 1;
    int blank = buffer.indexOf(' ', i, indexTo);
    if (blank == -1) {
        blank = indexTo;
    }
    int minor;
    try {
        minor = Integer.parseInt(buffer.substringTrimmed(i, blank));
    } catch (NumberFormatException e) {
        throw new ParseException("Invalid protocol minor version number: " + buffer.substring(indexFrom, indexTo));
    }
    cursor.updatePos(blank);
    return createProtocolVersion(major, minor);
}
Also used : ParseException(org.apache.http.ParseException)

Example 5 with ParseException

use of org.apache.http.ParseException in project robovm by robovm.

the class BasicTokenIterator method findTokenStart.

/**
     * Determines the starting position of the next token.
     * This method will iterate over headers if necessary.
     *
     * @param from      the position in the current header at which to
     *                  start the search
     *
     * @return  the position of the token start in the current header,
     *          negative if no token start could be found
     */
protected int findTokenStart(int from) {
    if (from < 0) {
        throw new IllegalArgumentException("Search position must not be negative: " + from);
    }
    boolean found = false;
    while (!found && (this.currentHeader != null)) {
        final int to = this.currentHeader.length();
        while (!found && (from < to)) {
            final char ch = this.currentHeader.charAt(from);
            if (isTokenSeparator(ch) || isWhitespace(ch)) {
                // whitspace and token separators are skipped
                from++;
            } else if (isTokenChar(this.currentHeader.charAt(from))) {
                // found the start of a token
                found = true;
            } else {
                throw new ParseException("Invalid character before token (pos " + from + "): " + this.currentHeader);
            }
        }
        if (!found) {
            if (this.headerIt.hasNext()) {
                this.currentHeader = this.headerIt.nextHeader().getValue();
                from = 0;
            } else {
                this.currentHeader = null;
            }
        }
    }
    return found ? from : -1;
}
Also used : ParseException(org.apache.http.ParseException)

Aggregations

ParseException (org.apache.http.ParseException)38 Header (org.apache.http.Header)14 ProtocolVersion (org.apache.http.ProtocolVersion)12 IOException (java.io.IOException)10 ProtocolException (org.apache.http.ProtocolException)9 HttpException (org.apache.http.HttpException)6 HttpParams (org.apache.http.params.HttpParams)6 Test (org.junit.Test)5 HttpEntity (org.apache.http.HttpEntity)4 Socket (java.net.Socket)3 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 SSLSocket (javax.net.ssl.SSLSocket)3 HeaderElement (org.apache.http.HeaderElement)3 HeaderIterator (org.apache.http.HeaderIterator)3 HttpConnection (org.apache.http.HttpConnection)3 HttpMessage (org.apache.http.HttpMessage)3 HttpResponse (org.apache.http.HttpResponse)3