问题描述:java代码调用接口没问题,不管post的数据是正确的还是错误,都能有响应,但改为c#代码后报错:The remote server returned an error: (400) Bad Request.
代码大致如下:
java代码:
//hostURI: https://agcod-gamma-cn.amazon.com/CreateGiftCard
URL hostConnection = new URL(hostURI);
HttpURLConnection conn = (HttpURLConnection)
hostConnection.openConnection();
conn.setRequestMethod("POST");
//contentType = "application/x-www-form-urlencoded; charset=UTF-8"; conn.setRequestProperty("accept", contentType);
conn.setRequestProperty("content-type", contentType);
// host = "agcod-gamma-cn.amazon.com";
conn.setRequestProperty("host", host);
// 20130826T051152Z 根据当前时间生成的一个字符串
conn.setRequestProperty("x-amz-date" , dateTimeString);
//com.amazon.agcod.AGCODService
conn.setRequestProperty("x-amz-target", serviceTarget);
//一个加密的字符串,
conn.setRequestProperty("authentication", authorizationValue);
conn.setDoOutput(true);
OutputStream output = conn.getOutputStream();
output.write(payload.toString().getBytes("UTF-8"));
BufferedReader in = null;
String inputLine;
if (conn.getResponseCode() != 200)
{ in = new BufferedReader(new InputStreamReader(conn.getErrorStream())); } else { in = new BufferedReader(new InputStreamReader(conn.getInputStream())); }
System.out.println("\nRESPONSE:\n");
while ((inputLine = in.readLine()) != null)
{ System.out.println(inputLine );}
转换的C#代码
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(hostURI);
request.Method = "POST";
request.Accept = contentType;
request.ContentType = contentType;
request.Host = host;
request.Headers.Add("x-amz-date", dateTimeString);
request.Headers.Add("x-amz-target", serviceTarget);
request.Headers.Add("authentication", authorizationValue);
Stream output = request.GetRequestStream();
byte[] contentBytes = Encoding.UTF8.GetBytes(payload.ToString());
output.Write(contentBytes, 0, contentBytes.Length);
output.Close();
string inputLine;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
当c#代码执行到最后一行的时候,报出400的错误:The remote server returned an error: (400) Bad Request.
大家谁知道,这是什么问题呢,谢谢大家的帮助!
用fiddler工具测试:
post:https://agcod-gamma-cn.amazon.com/CreateGiftCard
Host: agcod-gamma-cn.amazon.com
Accept: application/x-www-form-urlencoded; charset=UTF-8
ContentType: application/x-www-form-urlencoded; charset=UTF-8
x-amz-date: 20130826T054311Z
x-amz-target: com.amazon.agcod.AGCODService
Content-Length: 0
响应的结果是:
HTTP/1.1 400 Bad Request
x-amzn-RequestId: bb5ad14f-0e15-11e3-9d3e-53596d1cb5a2
Content-Type: text/xml
Content-Length: 138
Date: Mon, 26 Aug 2013 06:07:05 GMT
<AGCODException>
<Message>Invalid Date</Message>
<errorType>GeneralError</errorType>
<errorCode>F300</errorCode>
</AGCODException>
而c# HttpWebRequest调用直接抛出 400的异常。
抓包看下,两段代码发出去的请求有什么差异。
//contentType = "application/x-www-form-urlencoded; charset=UTF-8"; conn.setRequestProperty("accept", contentType);
conn.setRequestProperty("content-type", contentType);
你这个唱的是哪出戏?注释?
content-type非常重要,看看你的远程服务器是接受application/x-www-form-urlencoded类型还是application/json类型?
远程服务接收的是:application/x-www-form-urlencoded; charset=UTF-8
远程服务接收的是:application/x-www-form-urlencoded; charset=UTF-8
java代码中是完整的参数 // 后面的值是相应参数的值。
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
这行代码只要服务器返回任何不是200的。都认为是异常。异常代码中可加下面代码来捕获服务器返回的消息。
WebException web = e as WebException;
StreamReader intput = new StreamReader(web.Response.GetResponseStream());
Console.WriteLine("\nRESPONSE:\n");
string inputLine = string.Empty;
while ((inputLine = intput.ReadLine()) != null)
{
Console.WriteLine(inputLine);
}