Last active
August 29, 2015 14:27
-
-
Save proofy/264e957ba0cc190ee8d3 to your computer and use it in GitHub Desktop.
Getting String from HttpPost, useful for debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.joerns.web.global; | |
import java.io.Serializable; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.Header; | |
import org.apache.http.HeaderElement; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.methods.HttpPost; | |
public class HttpPostUtil implements Serializable { | |
public static String httpPostToString(HttpPost httppost) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("\nRequestLine:"); | |
sb.append(httppost.getRequestLine().toString()); | |
int i = 0; | |
for(Header header : httppost.getAllHeaders()){ | |
if(i == 0){ | |
sb.append("\nHeader:"); | |
} | |
i++; | |
for(HeaderElement element : header.getElements()){ | |
for(NameValuePair nvp :element.getParameters()){ | |
sb.append(nvp.getName()); | |
sb.append("="); | |
sb.append(nvp.getValue()); | |
sb.append(";"); | |
} | |
} | |
} | |
HttpEntity entity = httppost.getEntity(); | |
String content = ""; | |
if(entity != null){ | |
try { | |
content = IOUtils.toString(entity.getContent()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
sb.append("\nContent:"); | |
sb.append(content); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment