English 中文(简体)
奇怪的httpPost行为(可能是json、auth、代理相关的)GET有效,POST有效,但没有代理(部分解决)
原标题:Weird httpPost behavoir (may be json, auth, proxy related) GET works, POST works, but not without proxy (partially solved)

我编写了一个REST WCF RIA Silverlight 4.0兼容服务,可以从javascript+jQuery.1.4.2.js+JSON2.js(当然还有.NET 4.0应用程序)成功访问该服务

基本上,我在Android活动中所做的是:

            HttpPost request = new HttpPost(SERVICE_URI + "/SubmitChanges");             
        request.setHeader("Accept", "application/json; charset=utf-8");             
        request.setHeader("Content-type", "application/json; charset=utf-8");
        // Build JSON string            
        JSONStringer myTable = new JSONStringer()
        .object()
        .key("changeSet")
        .array()
            .object()
            .key("Id").value(0)
            .key("Entity")
                .object()
                    .key("__type").value("MyTable:#MyServiceApp.Web")
                    .key("ID").value(21)
                .endObject()
            .key("Operation").value(4)
            .endObject()
        .endArray()
        .endObject();
        Log.d("WebInvoke", "MyTable : " + myTable.toString());                           
        StringEntity entity = new StringEntity(myTable.toString());

//          entity.setContentType("application/json; charset=utf-8"); 
//          entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8")); 
            request.setEntity(entity);
            Log.d("WebInvoke", "Entity : " + entity.toString());                           
            // Send request to WCF service             
            DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(null,-1), new UsernamePasswordCredentials("myuser","mypassword"));
//          HttpClientParams myParam = (HttpClientParams) httpClient.getParams();
//          Log.d("WebInvoke", "Entity : " + myParam.toString());                           

            HttpResponse response = httpClient.execute(request);              

When turned to Android app, I noted that I cannot do PUT s operations (GETs worked OK). They returned error 400 - Bad request As it worked OK on js, I used fiddler2 to see what s happend. When I setup the emulator to work throught fiddler proxy, suddenly, the request has turned OK. Then I tryed again without proxy, and it returned to error 400. I tryed also uploading the apk to my Galaxy S and connecting through web. Again, GETs works, but PUTs don t...

有什么想法吗?

奥斯卡,萨鲁多斯。

更新

这是上一次在没有代理的情况下执行请求

request HttpPost  (id=830085521536) 
    aborted false
    abortLock   ReentrantLock  (id=830085616656)
    connRequest null
    entity  StringEntity  (id=830085531688)
    headergroup HeaderGroup  (id=830085644864)
    params  null
    releaseTrigger  null
    uri URI  (id=830085878840)
httpClient  DefaultHttpClient  (id=830085612544)    
    connManager null
    cookieStore BasicCookieStore  (id=830085604904)
    credsProvider   BasicCredentialsProvider  (id=830085612624)
    defaultParams   null
    httpProcessor   null
    keepAliveStrategy   null
    log Jdk14Logger  (id=830085596488)
    proxyAuthHandler    null
    redirectHandler null
    requestExec null
    retryHandler    null
    reuseStrategy   null
    routePlanner    null
    supportedAuthSchemes    null
    supportedCookieSpecs    null
    targetAuthHandler   null
    userTokenHandler    null

这是在没有代理的情况下执行请求的后续操作

request HttpPost  (id=830085521536) 
    aborted false
    abortLock   ReentrantLock  (id=830085616656)
    connRequest null
    entity  StringEntity  (id=830085531688)
    headergroup HeaderGroup  (id=830085644864)
    params  BasicHttpParams  (id=830085486480)
    releaseTrigger  SingleClientConnManager$ConnAdapter  (id=830085855888)
    uri URI  (id=830085878840)
httpClient  DefaultHttpClient  (id=830085612544)    
    connManager SingleClientConnManager  (id=830085927648)
    cookieStore BasicCookieStore  (id=830085604904)
    credsProvider   BasicCredentialsProvider  (id=830085612624)
    defaultParams   BasicHttpParams  (id=830085575968)
    httpProcessor   BasicHttpProcessor  (id=830085617496)
    keepAliveStrategy   DefaultConnectionKeepAliveStrategy  (id=830085544576)
    log Jdk14Logger  (id=830085596488)
    proxyAuthHandler    DefaultProxyAuthenticationHandler  (id=830085951384)
    redirectHandler DefaultRedirectHandler  (id=830085524448)
    requestExec HttpRequestExecutor  (id=830085525992)
    retryHandler    DefaultHttpRequestRetryHandler  (id=830085583064)
    reuseStrategy   DefaultConnectionReuseStrategy  (id=830085882544)
    routePlanner    DefaultHttpRoutePlanner  (id=830085880096)
    supportedAuthSchemes    AuthSchemeRegistry  (id=830085650400)
    supportedCookieSpecs    CookieSpecRegistry  (id=830085516320)
    targetAuthHandler   DefaultTargetAuthenticationHandler  (id=830085583264)
    userTokenHandler    DefaultUserTokenHandler  (id=830085486432)
response    BasicHttpResponse  (id=830085980112)    
    entity  BasicManagedEntity  (id=830085983552)
    headergroup HeaderGroup  (id=830085625184)
    locale  Locale  (id=830085944016)
    params  ClientParamsStack  (id=830085486448)
    reasonCatalog   EnglishReasonPhraseCatalog  (id=830004383520)
    statusline  BasicStatusLine  (id=830085625160)

这是使用代理执行请求的前一步

request HttpPost  (id=830085966120) 
    aborted false   
    abortLock   ReentrantLock  (id=830085966520)    
    connRequest null    
    entity  StringEntity  (id=830085971592) 
    headergroup HeaderGroup  (id=830085966392)  
    params  null    
    releaseTrigger  null    
    uri URI  (id=830085966568)  
httpClient  DefaultHttpClient  (id=830085973896)    
    connManager null    
    cookieStore BasicCookieStore  (id=830085974504) 
    credsProvider   BasicCredentialsProvider  (id=830085974144) 
    defaultParams   null    
    httpProcessor   null    
    keepAliveStrategy   null    
    log Jdk14Logger  (id=830085974120)  
    proxyAuthHandler    null    
    redirectHandler null    
    requestExec null    
    retryHandler    null    
    reuseStrategy   null    
    routePlanner    null    
    supportedAuthSchemes    null    
    supportedCookieSpecs    null    
    targetAuthHandler   null    
    userTokenHandler    null    

这是在使用代理执行请求之后

request HttpPost  (id=830085966120) 
    aborted false   
    abortLock   ReentrantLock  (id=830085966520)    
    connRequest null    
    entity  StringEntity  (id=830085971592) 
    headergroup HeaderGroup  (id=830085966392)  
    params  BasicHttpParams  (id=830085980824)  
    releaseTrigger  SingleClientConnManager$ConnAdapter  (id=830085981992)  
    uri URI  (id=830085966568)  
httpClient  DefaultHttpClient  (id=830085973896)    
    connManager SingleClientConnManager  (id=830085978144)  
    cookieStore BasicCookieStore  (id=830085974504) 
    credsProvider   BasicCredentialsProvider  (id=830085974144) 
    defaultParams   BasicHttpParams  (id=830085977232)  
    httpProcessor   BasicHttpProcessor  (id=830085978848)   
    keepAliveStrategy   DefaultConnectionKeepAliveStrategy  (id=830085978816)   
    log Jdk14Logger  (id=830085974120)  
    proxyAuthHandler    DefaultProxyAuthenticationHandler  (id=830085980560)    
    redirectHandler DefaultRedirectHandler  (id=830085980152)   
    requestExec HttpRequestExecutor  (id=830085976912)  
    retryHandler    DefaultHttpRequestRetryHandler  (id=830085980128)   
    reuseStrategy   DefaultConnectionReuseStrategy  (id=830085978800)   
    routePlanner    DefaultHttpRoutePlanner  (id=830085978832)  
    supportedAuthSchemes    AuthSchemeRegistry  (id=830085975792)   
    supportedCookieSpecs    CookieSpecRegistry  (id=830085976320)   
    targetAuthHandler   DefaultTargetAuthenticationHandler  (id=830085980344)   
    userTokenHandler    DefaultUserTokenHandler  (id=830085980776)  
response    BasicHttpResponse  (id=830085927240)    
    entity  BasicManagedEntity  (id=830085863688)   
    headergroup HeaderGroup  (id=830085944256)  
    locale  Locale  (id=830085944272)   
    params  ClientParamsStack  (id=830085980792)    
    reasonCatalog   EnglishReasonPhraseCatalog  (id=830004383520)   
    statusline  BasicStatusLine  (id=830085927216)  
问题回答

作为一个与协议有关的问题,我开始在服务器中测试各种认证方案。似乎自2008年以来,java和apache都不支持在新服务器中实现的新NTLMv2。

我发现(并丢失)了一个网络,其中注意到使用JCIFS来带来差距。但事情并不是那么简单,包括jar,你还必须编写部分代码,并支持所有不确定和不谨慎的东西。这意味着,在实际的行话中,只使用基本的身份验证,但使用大写字母。。。

顺便说一句,我找到了它的工作方式。它只是简单地添加了以下内容:

httpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

Dave, I owe you one big! Many thanks to every one for read the thread!

奥斯卡,萨鲁多斯。

您是否尝试过在StringEntity上显式设置字符集

import org.apache.http.protocol.HTTP;
...
StringEntity entity = new StringEntity(myTable.toString(),HTTP.UTF_8);

对于任何提到这个问题的人(大约在被问到一年后),我也遇到了类似的问题,代码几乎相同。问题似乎出在setCredentials()方法上。显然,它包含了一些可以使用GET但会导致PUT或POST出现问题的空白。

手动设置授权应该可以解决这个问题。类似于:

request.setHeader("Authorization","Basic "+Base64.encodeToString("login:password".getBytes(),Base64.URL_SAFE|Base64.NO_WRAP););

请记住使用Base64.URL_SAFE|Base64.NO_WRAP

参考:android上授权的HTTP POST请求





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签