HttpClient client = new HttpClient()
String url = ana/workFlowOaInterface.do?action=oaTask
PostMethod postMethod = new PostMethod(url)
//设置参数编码为gbk
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk")
//构造键值对参数
NameValuePair[] data = { new NameValuePair("processInstanceId", "230"), new NameValuePair("approveFlag", "1") }
// 把参数值放入postMethod中
postMethod.setRequestBody(data)
//执行
client.executeMethod(postMethod)
//读取内容
byte[] responseBody = postMethod.getResponseBody()
//处理内容
System.out.println(new String(responseBody))
System.out.println("getStatusLine:"+postMethod.getStatusLine())
System.out.println("~~~"+postMethod.getResponseBodyAsString())
System.out.println("statusCode:"+statusCode)
//打印结果页面
String response = new String(postMethod.getResponseBodyAsString().getBytes("utf-8"))
//打印返回的信息
System.out.println("response:"+response)
//释放连接
postMethod.releaseConnection()
如果传递的是中文参数,有可能会出现乱码,通过设置正确的参数编码来解决。
在被调用的接口方,直接通过request.getParameter的方式获取。
可以把你的这段代码简化一下<pre t="code" l="java">ins = method.getResponseBodyAsStream()byte[] b = new byte[1024]
int r_len = 0
while ((r_len = ins.read(b)) >0)
{
result.append(new String(b, 0, r_len, method.getResponseCharSet()))
}替换为:
<pre t="code" l="java">byte[] ba = method.getResponseBody()
String str= new String(ba,"UTF-8") //得到指定编码格式的字符串这里的str就是你的返回值。这段代码我使用过,可以解决乱码问题
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)