2017-01-17 21 views
0

私はです.HttpURLConnectionを使用してWebサーバーにPUT http要求を作成しました。私はPUTリクエストをうまく作成するいくつかのコードを持っていますが、ヘッダに 'Expect 100-Continue Request Property'を含めることはできますが、私はその機能を '100 Continue '実際のHTTPペイロードを送信する前に、サーバーからの応答。HttpURLConnectionを使用してJavaでExpect 100-continue応答を待つ方法

私は

PUT /post/ HTTP/1.1 
User-Agent: curl/7.35.0 
Accept: */* 
Content-Type: application/x-www-form-urlencoded 
Expect: 100-continue 
Host: somerandomdomain.info 
Connection: keep-alive 
Content-Length: 17 

Some data for you 
HTTP/1.1 100 Continue 

...rest of web-server response... 

(Wiresharkのから)、次の取得私は空白を描いたグーグルで後が何かを明らかに行方不明です確信している - 誰でも助けることができますか?

多くのおかげで、以下の場合はそう:)

のHttp PUTコードスニペット:

String url = "http://somerandomdomain.info"; 
String postJsonData = "Some data for you\n"; 
URL obj = new URL(url); 
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

// Setting basic post request 
con.setRequestMethod("PUT"); 
con.setRequestProperty("User-Agent", "jcurl/7.35.0"); 
con.setRequestProperty("Accept", "*/*"); 
con.setRequestProperty("Content-Length", postData.length() + ""); 
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
con.setRequestProperty("Expect", "100-continue"); 

// Send post request 
con.setDoOutput(true); 
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
wr.writeBytes(postData); 
wr.flush(); 
wr.close(); 

int responseCode = con.getResponseCode(); 
System.out.println("\nSending 'POST' request to URL : " + url); 
System.out.println("Post Data : " + postData); 
System.out.println("Response Code : " + responseCode); 

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream())); 
String output; 
StringBuffer response = new StringBuffer(); 

while ((output = in.readLine()) != null) { 
    response.append(output); 
} 

in.close(); 

//printing result from response 
System.out.println(response.toString()); 

答えて

1

それは、彼らはそれをこのように設計された理由は私には明らかではないが、期待し実装するコード:100ロジックがありますgetOutputStreamを実行する前にsetFixedLengthStreamingMode(int contentlen)またはthe overload for longまたはsetChunkedStreamingMode(int chunklen)のいずれかを呼び出した場合にのみ使用されます。この場合、私は最初のものを最も単純なものとして推奨します。

+0

Javaは、これらのメソッドのどちらも使用されていない場合、Content-Lengthヘッダー自体を設定します。ユーザーが 'setRequestProperty(" Content-length "、...)';アプリケーションによってすべてが書き込まれるまで、この場合のコンテンツの長さはわかりません(隠された 'ByteArrayOutputStream')。アプリケーションが出力ストリームを閉じた後にのみ待機が発生する可能性があります。たぶん彼らは、最終的に試行されたときにサーバーによって拒否されるかもしれないBAOSへの書き込みの任意の長いシーケンスを許可することは、無駄な部分にちょっとしたものでした。 – EJP

関連する問題