本文整理汇总了Java中org.apache.commons.httpclient.methods.PostMethod.getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:Java PostMethod.getStatusCode方法的具体用法?Java PostMethod.getStatusCode怎么用?Java PostMethod.getStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.httpclient.methods.PostMethod
的用法示例。
在下文中一共展示了PostMethod.getStatusCode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: ChunkEncodedPost <file>");
System.out.println("<file> - full path to a file to be posted");
System.exit(1);
}
HttpClient client = new HttpClient();
PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");
File file = new File(args[0]);
httppost.setRequestEntity(new InputStreamRequestEntity(
new FileInputStream(file), file.length()));
try {
client.executeMethod(httppost);
if (httppost.getStatusCode() == HttpStatus.SC_OK) {
System.out.println(httppost.getResponseBodyAsString());
} else {
System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
}
} finally {
httppost.releaseConnection();
}
}
示例2: main
import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: ChunkEncodedPost <file>");
System.out.println("<file> - full path to a file to be posted");
System.exit(1);
}
HttpClient client = new HttpClient();
PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");
File file = new File(args[0]);
httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
httppost.setContentChunked(true);
try {
client.executeMethod(httppost);
if (httppost.getStatusCode() == HttpStatus.SC_OK) {
System.out.println(httppost.getResponseBodyAsString());
} else {
System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
}
} finally {
httppost.releaseConnection();
}
}
示例3: postQuery
import org.apache.commons.httpclient.methods.PostMethod; //导入方法依赖的package包/类
protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body) throws UnsupportedEncodingException,
IOException, HttpException, URIException, JSONException
{
PostMethod post = new PostMethod(url);
if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER)
{
post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
}
post.setRequestEntity(new ByteArrayRequestEntity(body.toString().getBytes("UTF-8"), "application/json"));
try
{
httpClient.executeMethod(post);
if(post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
{
Header locationHeader = post.getResponseHeader("location");
if (locationHeader != null)
{
String redirectLocation = locationHeader.getValue();
post.setURI(new URI(redirectLocation, true));
httpClient.executeMethod(post);
}
}
if (post.getStatusCode() != HttpServletResponse.SC_OK)
{
throw new LuceneQueryParserException("Request failed " + post.getStatusCode() + " " + url.toString());
}
Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
// TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
JSONObject json = new JSONObject(new JSONTokener(reader));
if (json.has("status"))
{
JSONObject status = json.getJSONObject("status");
if (status.getInt("code") != HttpServletResponse.SC_OK)
{
throw new LuceneQueryParserException("SOLR side error: " + status.getString("message"));
}
}
return json;
}
finally
{
post.releaseConnection();
}
}