vladukua / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Ability to set zero-length content on an HTTP GET request #130

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-api-java-client (e.g. 1.2.1-alpha)?
1.3.1-alpha

Java environment (e.g. Java 6, Android 2.2, App Engine 1.3.7)?
Java 6

Describe the problem.
As  yan...@google.com concerned in Issue 68 ,"Please don't use "content-type" 
header directly and instead use an instance of HttpContent.If the HttpContent 
of a request is null, we will assume there is no content."

but how to make an instance httpcontent? For example I need make request as 
this: 
Content-Length=0 Content-Type=text/plain.which httpcontent should I use 
GZip,urlencode or logcontent?

the other way is use NetHttpRequest,but I can't use NetHttpRequest.setcontent().

HttpRequest request = transport.buildGetRequest();is correct,but HttpRequest 
doesn't have method setcontent()

NetHttpRequest request = transport.buildGetRequest(); It said "NetHttpRequest 
cannot be resolved to a type".When I
import com.google.api.client.http.javanet.NetHttpRequest; It said "The type 
com.google.api.client.http.javanet.NetHttpRequest is not visible."
The declaration of NetHttpQequest is 
final class NetHttpRequest extends LowLevelHttpRequest 

How would you expect it to be fixed?
would mend give a sample of googlestorage?

here is my code:
public GoogleStorage(String accessKey,String secret) throws IOException
    {
                //work for fiddle
        //System.setProperty("http.proxyHost", "localhost");
        //System.setProperty("http.proxyPort", "8888");
        //System.setProperty("https.proxyHost", "localhost");
        //System.setProperty("https.proxyPort", "8888");

        transport = new NetHttpTransport();
         GoogleHeaders headers=new GoogleHeaders();
         transport.defaultHeaders=headers;
                 //Is this Parser right ? I also want use xmlparser to parser 
                 //response err message.
         transport.addParser(new JsonCParser());
         GoogleStorageAuthentication.authorize(transport, accessKey, secret);

    }

    public void list() throws IOException
    {

        HttpRequest request = transport.buildGetRequest();

        request.setUrl("http://commondatastorage.googleapis.com");
        request.headers.put("Date",Now());

        //this is not work correct
        //request.headers.put("Content-Length","0");
        //request.headers.put("Content-Type","text/plain");

        try {
            HttpResponse response = request.execute();
            System.out.println(response.parseAsString());
        }catch (HttpResponseException e){
                //this is not work correct ,how to parse response err message?
            System.out.println(e.response.parseAsString()));
        };

Original issue reported on code.google.com by zcmk...@gmail.com on 24 Feb 2011 at 3:13

GoogleCodeExporter commented 9 years ago
Try this:
        InputStreamContent content = new InputStreamContent();
        content.setByteArrayInput(new byte[0]);
        content.type = "text/plain";
        request.content = content;

Original comment by yan...@google.com on 24 Feb 2011 at 3:52

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thank you for the answer.I tried the code,it seems still doesn't work correct.
my code like this:
public void list() throws IOException
    {

        HttpRequest request = transport.buildGetRequest();
        request.setUrl("http://commondatastorage.googleapis.com");

        InputStreamContent content = new InputStreamContent();
            content.setByteArrayInput(new byte[0]);
            content.type = "text/plain";
            request.content = content;

            request.headers.put("Date",Now());

        try {
            HttpResponse response = request.execute();
            System.out.println(response.parseAsString());
        }catch (HttpResponseException e){
            System.out.println(e.response.parseAsString());
        };

    }
I use fiddle catch to package, include your code the Httpmethod is post 
del the code  httpmethod is correct :get.
I trace the code in the httprequest.class ,the httpmethod still is "get"
is there a point think content has a inputstream then set httpmethod to post?
by the way if not set content googlestoageauthorize doesn't work correct.
another silly question:how to parser the googlestorage error status,
I found the code in issue 9:
 XmlHttpParser errorParser = new XmlHttpParser();
   errorParser.contentType = "application/vnd.google.gdata.error+xml";
   errorParser.namespaceDictionary = errorNamespaceDictionary;
   transport.addParser(errorParser);
but errorNamespaceDictionary isn't valid.And doest is need to design a 
errorstatus class to parser it?

Original comment by zcmk...@gmail.com on 24 Feb 2011 at 1:43

GoogleCodeExporter commented 9 years ago
There seems has a bug.I traced in httprequest.java:
HttpResponse response = new HttpResponse(transport, 
lowLevelHttpRequest.execute());
before this lowLevelHttpRequest.connection.method is GET
after this lowLevelHttpRequest.connection.method is POST 

Original comment by zcmk...@gmail.com on 27 Feb 2011 at 8:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
In  NetHttpRequest.java ,public LowLevelHttpResponse execute():
content.writeTo(connection.getOutputStream());
if you call connection.getOutputStream(),connection.method change from "GET" to 
"POST";
since all GET content-length=0, I suggest the code to this:
if (content != null) {
      connection.setDoOutput(true);
      String contentType = content.getType();
      if (contentType != null) {
        addHeader("Content-Type", contentType);
      }
      String contentEncoding = content.getEncoding();
      if (contentEncoding != null) {
        addHeader("Content-Encoding", contentEncoding);
      }
      long contentLength = content.getLength();
      if (contentLength >= 0) {
        addHeader("Content-Length", Long.toString(contentLength));
      }
      if (contentLength > 0) {
        connection.setDoOutput(true);
        content.writeTo(connection.getOutputStream());
      }
    }

Original comment by zcmk...@gmail.com on 14 Mar 2011 at 11:38

GoogleCodeExporter commented 9 years ago
Need to try to reproduce it.

Original comment by yan...@google.com on 22 Mar 2011 at 2:26

GoogleCodeExporter commented 9 years ago
Taking a look at the JavaDoc for HttpURLConnection on Android:

http://developer.android.com/reference/java/net/HttpURLConnection.html

It says that calling setDoOutput(true) changes the method from GET to POST.

Original comment by yan...@google.com on 5 Apr 2011 at 7:31

GoogleCodeExporter commented 9 years ago
http://codereview.appspot.com/4354051/

Original comment by yan...@google.com on 5 Apr 2011 at 7:53

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 5 Apr 2011 at 7:56