1! - Цитата | System.out.println("!!!!!! - Код ответа: "+rc); |
что выдает эта строка на экран? 2! - Цитата | c.setRequestProperty("User-Agent", "тест"); |
Если мидлет не подписан - этот параметр передаваться не будет, точнее будет как untrusted, Вывод - убери эту строку 3! - Цитата | c.setRequestProperty("Content-Language", "en-US"); |
Убери это тоже. 4! -
Цитата | c.setRequestProperty("content-length", String.valueOf(post.length()));
|
Попробуй "Content-Length" написать именно так. 5! - Цитата | String.valueOf(post); |
Эта строка вроде как не нужна. 6! - Так же есть такое свойтсво - с.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
Пример отсылки запроса(я уже вроде как где-то описывал) -
Код | /** * Creates Entity-Body of post request * * @param boundary * @param parameters Hashtable of parameters ("key" - name of parameter, * "value" - value of parameter) * @return <description> */ private static byte[] getMultiPart(String boundary, Hashtable parameters) { String crlf = "\r\n"; StringBuffer sb = new StringBuffer(); Enumeration e = parameters.keys(); while (e.hasMoreElements()) { String nameOfParameter = (String) e.nextElement(); String valueOfParameter = (String) parameters.get(nameOfParameter); sb.append("--" + boundary + crlf); sb.append("Content-Disposition: form-data; name=\"" + nameOfParameter + "\"" + crlf); sb.append(crlf); sb.append("" + valueOfParameter + crlf); } sb.append("--" + boundary + "--" + crlf); return sb.toString().getBytes(); }
|
Вот пример логина -
Код | /** * * @param login * @param password * @param network * @throws IOException */ public static void buildLoginRequest(String login, String password, String network) throws IOException { String boundary = "1B" + System.currentTimeMillis(); Hashtable parameters = new Hashtable(); parameters.put(Constants.REQUEST_PROPERTY_TYPE, Constants.REQUEST_NAME_LOGIN); parameters.put(Constants.REQUEST_FIELD_NAME_USER_NAME, login); parameters.put(Constants.REQUEST_FIELD_NAME_PASSWORD, password); parameters.put(Constants.REQUEST_FIELD_NAME_NETWORK, network); byte[] data = getMultiPart(boundary, parameters); sendRequestParseResponse(data, boundary); }
|
Код | /** * * @param data * @param boundary * @throws IOException */ private static void sendRequestParseResponse(byte[] data, String boundary) throws IOException { byte[] responseData; try { responseData = HttpSender.getInstance().send(Constants.URL, data, boundary); } catch (IOException ioe) { Logger.logError(className, "sendRequestParseResponse " + ioe.getMessage());//этот класс можно описать самому throw new IOException(ioe.getMessage()); } XMLParser.parseResponse(responseData);//это я дальше парсил ответ, этого класса в примере нет }
|
непосредственно HttpSender
Код | /** * @author Alexander Lonsky */
import src.utils.Logger; import src.model.Constants;
import javax.microedition.io.Connector; import javax.microedition.io.ConnectionNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.InputStream; import java.io.ByteArrayOutputStream; import javax.microedition.io.HttpConnection;
public class HttpSender { private static HttpSender httpSender = null;
private HttpSender() { }
public static synchronized HttpSender getInstance() { if (httpSender == null) { httpSender = new HttpSender(); } return httpSender; }
/** * - * * @param url * @param dataOutput if dataOutput == null - get request else post request * @return response data */ public synchronized byte[] send(String url, byte[] dataOutput, String boundary) throws IOException { if (url == null) { return null; } HttpConnection hCon; ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); try { hCon = (HttpConnection) Connector.open(url); if (dataOutput != null) { hCon.setRequestMethod(HttpConnection.POST); hCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); hCon.setRequestProperty("Content-Length", String.valueOf(dataOutput.length)); OutputStream os; os = hCon.openOutputStream(); os.write(dataOutput); os.close(); } } catch (ConnectionNotFoundException cnfe) { Logger.logError(getClass().getName(), "send(1) : " + Constants.ERROR_TYPE_CONNECTION_NOT_FOUND); throw new IOException(Constants.ERROR_TYPE_CONNECTION_NOT_FOUND); } catch (IOException ioe) { Logger.logError(getClass().getName(), "send(2) : openURL ioe exception" + ioe.getMessage()); throw new IOException("openURL ioe exception"); } catch (ClassCastException cce) { Logger.logError(getClass().getName(), "send(3) : Bad url exception"); throw new IOException("Bad url exception"); } catch (SecurityException se) { Logger.logError(getClass().getName(), "send(4) : Permissions denied exception"); throw new IOException("Permissions denied exception"); } catch (NullPointerException npe) { Logger.logError(getClass().getName(), "send(5) : "); throw new IOException("NullPointer exception"); } if (hCon.getResponseCode() == HttpConnection.HTTP_OK) { InputStream is; is = hCon.openInputStream(); int ch; try { while ((ch = is.read()) != -1) { bStrm.write(ch); } bStrm.close(); is.close(); hCon.close(); } catch (IOException ioe) { Logger.logError(getClass().getName(), "can't read from stream"); } } else { Logger.logError(getClass().getName(), String.valueOf(hCon.getResponseCode()) + " : " + hCon.getResponseMessage()); throw new IOException(String.valueOf(hCon.getResponseCode()) + " : " + hCon.getResponseMessage()); } return bStrm.toByteArray(); }
}
|
|