views:

69

answers:

1

"Basic YWRtaW46YW RtaW4=" is right code for my address.I check on j2me project.

And on android my getbase64 method returns "Basic YWRtaW46YW RtaW4=" its true.

and ı use it on:

httpConnection.setRequestProperty("Authorization",getBase64Encode());

Finally responce code is 401

any idea???

protected void connect() {
  InputStream is = null;
  OutputStream os = null;
  try {

   url = new URL(getUrl());
   System.out.println(getUrl());// duzelt
   queryString = encodeURL(queryString);
   byte postmsg[] = queryString.getBytes("UTF-8");
   conn = url.openConnection();
   if (!(conn instanceof HttpURLConnection))
    throw new IOException("Not an HTTP connection");

   httpConnection = (HttpURLConnection) conn;
   HttpOptions options=new HttpOptions();
   httpConnection.setDoInput(true);
   httpConnection.setDoOutput(true);
   httpConnection.setRequestMethod("GET");
   httpConnection.setRequestProperty("Authorization",
     getBase64Encode());
   httpConnection.connect();
   os = httpConnection.getOutputStream();// ///////////////////baglantının
   System.out.println("response code: "+ httpConnection.getResponseCode());
   // connect olup olmadıgını
   // kontrol et

   for (int i = 0; i < postmsg.length; i++) {
    os.write(postmsg[i]);
   }
   if (!cancel) {
    onReturn(httpConnection.getResponseCode(), httpConnection
      .getInputStream());
   }
   os.close();
   // httpConnection.close();
  } catch (Exception e) {
   System.out.println(e.getMessage());
   try {
    httpConnection.disconnect();
    Thread.sleep(60);
    // cancel=true; eklenmesı gerekebilir
   } catch (Exception ie) {
   }
   onError(e);
  }
+1  A: 

There is a setConnectTimeout(int) method on HttpURLConnection.

Sets the timeout value in milliseconds for establishing the connection to the resource pointed by this URLConnection instance. A SocketTimeoutException is thrown if the connection could not be established in this time. Default is 0 which stands for an infinite timeout.

httpConnection.setConnectTimeout(10000); /* connection timeout set to 10s */
Jcs
yes, ı tried it but there is no difference..:( I can show Mjpeg fıle but ı cant send a link:(( after ı tried connection with httpget-defaulthttpclıent, and it works. What are there different between them??
atasoyh
Different implementations leads to different behaviours :) Maybe connection timeout and read timeout (setReadTimeout) should be set in order to handle the case when the server accepts the connection but does not answer to the request.
Jcs
I finally used httpclient, no problem..
atasoyh