Android:HTTPCLIENT POST - Cookies被拒绝

时间:2012-06-05 12:35:34

标签: android httpclient gsoap

我在运行HttpClient示例时经常收到警告

 06-05 17:43:40.568: W/ResponseProcessCookies(725): Cookie rejected:   "BasicClientCookie[version=1,name=visit,domain=.127.0.0.1,path=/,expiry=Tue Jun 05 17:53:40 IST 2012]". Domain attribute ".127.0.0.1" violates RFC 2965: effective host name does not domain-match domain attribute.

我正在尝试将数据发送到gsoap服务器。我已经完成了 link1 link2 link3但是没有得到很多帮助。 这是我的HttpClient邮政编码的代码

public class newA extends Activity implements OnClickListener {

private static final String TAG = "MyPost";

private boolean post_is_running = false;

private doSomethingDelayed doSth;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Button pushButton = (Button) findViewById(R.id.push_button);
    pushButton.setOnClickListener(this);

}

@Override
protected void onPause() {
    super.onPause();
    if (post_is_running) { // stop async task if it's running if app gets
                            // paused
        Log.v(TAG, "Stopping Async Task onPause");
        doSth.cancel(true);
    }
}

@Override
protected void onResume() {
    super.onResume();
     if (post_is_running) { // start async task if it was running previously
                            // and was stopped by   onPause()
        Log.v(TAG, "Starting Async Task onResume");
        doSth = (doSomethingDelayed) new doSomethingDelayed().execute();
        ((Button) findViewById(R.id.push_button)).setText("Resuming..");
    }
}

public void onClick(View v) {
    if (post_is_running == false) {
        post_is_running = true;
        Log.v(TAG, "Starting Async Task onClick");
        doSth = (doSomethingDelayed) new doSomethingDelayed().execute();

        ((Button) findViewById(R.id.push_button)).setText("Starting..");
    } else {
        Log.v(TAG, "Stopping Async Task onClick");
        post_is_running = false;
        doSth.cancel(true);
        ((Button) findViewById(R.id.push_button)).setText("Stopping..");
    }
}

private class doSomethingDelayed extends AsyncTask<Void, Integer, Void> {

    private int num_runs = 0;

    @Override
    protected Void doInBackground(Void... gurk) {

        while (!this.isCancelled()) {
            Log.v(TAG, "going into postData");

            long ms_before = SystemClock.uptimeMillis();
            Log.v(TAG, "Time Now is " + ms_before);

            postData();

            long ms_after = SystemClock.uptimeMillis();

            long time_passed = ms_after - ms_before;

            Log.v(TAG, "coming out of postData");
            Log.i(TAG, "RTT: " + time_passed + " ms");

            num_runs++;

            // publish to UI
            if (!this.isCancelled()) {
                publishProgress(num_runs, (int) time_passed);
            }
        }
        return null;
    }

    @Override
    protected void onCancelled() {
        Context context = getApplicationContext();
        CharSequence text = "Cancelled BG-Thread";
        int duration = Toast.LENGTH_LONG;

        Toast.makeText(context, text, duration).show();

        ((Button) findViewById(R.id.push_button))
                .setText("Stopped. Tap to Start!");
    }

    @Override
    protected void onProgressUpdate(Integer... num_runs) {
        Context context = getApplicationContext();
        CharSequence text = "Looped " + num_runs[0].toString() + " Times";
        int duration = Toast.LENGTH_SHORT;

        Toast.makeText(context,
                text + "\nRTT: " + num_runs[1].toString() + " ms", duration)
                .show();

        ((Button) findViewById(R.id.push_button)).setText(text
                + "\nTap to Stop");

    }
}

/**
 * stupid function that posts hardcoded data to hardcoded address
 */

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    // HttpPost httppost = new HttpPost("http://129.132.131.73:8880/form");
    HttpPost httppost = new HttpPost("http://192.168.1.37");
    // HttpPost httppost = new HttpPost("http://disney.com/");
    // HttpGet httppost = new HttpGet("http://192.168.1.137:8880/form");

    String resp = null;
    long time_passed = 0;
    try {
        // create data to POST
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata",
                "XXXXX Pvt Ltd!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        long ms_before = SystemClock.uptimeMillis();

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        long ms_after = SystemClock.uptimeMillis();
        time_passed = ms_after - ms_before;

        resp = response.toString();

    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }

    Log.i(TAG, "RTT inside post-data: " + time_passed);

    Log.i(TAG, resp.toString());
}

}

请让我知道问题出在哪里..谢谢..

1 个答案:

答案 0 :(得分:1)

我认为您的HTTPClient / Android代码很好。看起来HTTP服务器正在尝试为域“.127.0.0.1”发送cookie,但这是基于IP地址而不是域名。

您可能需要将HTTP / SOAP服务器配置为相信它具有可从您的网络访问的真实名称,而不是127.0.0.1或其他IP地址。此可能也有效:37.1.168.192.in-addr.arpa

配置服务器后,您可能需要更改Java代码以使用该域名(例如new HttpPost("http://server-name.local/");)。