在J2me中有400个错误请求

时间:2012-08-07 12:05:55

标签: http java-me midp

我正在尝试使用POST方法从我的JME应用程序中获取.svc服务。但得到'糟糕的要求'。以下是我的代码。

HttpConnection hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
hc.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
hc.setRequestProperty("Content-Length", ""+(postMsg.getBytes().length));

out = hc.openOutputStream();
out.write(postMsg.getBytes());

System.out.println("hc.getResponseCode() = "+hc.getResponseCode()+ "  hc.getResponseMessage() = "+hc.getResponseMessage());

请告诉我代码有什么问题。

1 个答案:

答案 0 :(得分:0)

我使用Ksoap2-j2me-core jar代替http,而不是http,我找到了以下代码 -

    SoapObject request = new SoapObject("namespace", "login");   

    request.addProperty("username", "pranav");   
    request.addProperty("password", "gangan");   


    //create the SOAP envelope   
    final SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);   
    env.setOutputSoapObject(request);   

    //create the transport and then call   
    final HttpTransport httpTransport = new HttpTransport("http://URL");   
    httpTransport.call("\"login\"", env);   

    SoapObject body = (SoapObject) env.bodyIn;   

    //body.getProperty(0) will return the content of the first tag inside body   
    Object response = body.getProperty(0);   
            System.out.println(response.toString);