我再一次需要你的帮助解决问题:
我目前正在实施一个与网络电源插座通信的应用。因此我发送了一个“自制”的http请求。只要我使用WiFi或使用o2作为提供商(我住在德国),这种方法就可以正常工作。 但是一旦我使用沃达丰的移动互联网,我就会得到废话作为回应。
以下是我的所作所为:
checkDosenTimer: function(){
var header =
"GET /dd.htm?DD" + dosenId + " HTTP/1.1\r\n"
+"Host: " + ip + "\r\n"
+"Connection: keep-alive\r\n"
+"Authorization: Basic " + Base64.encode(userName+":"+password) + "\r\n"
+"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
+"User-Agent: Mozilla/5.0 (Linux; Android 4.1.1; HTC One S Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Mobile Safari/537.36\r\n"
+"Referer: http://" + ip + "mobile.htm\r\n"
+"Accept-Encoding: gzip,deflate,sdch\r\n"
+"Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
+"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3;\r\n\r\n";
var that = this;
cordova.exec(
function(succ){
that.processTimerResult(succ);
},
function(err){
console.log("Error bei HTTPController sendMessage: "+err);
},
"HttpController",
"sendMessage",
[header,'',ip,httpPort]
);
}
所以我做的是,我在JavaScript中设置我的标头并将其传递给HttpController中的Java方法sendMessage
,该方法具有以下形式:
private void sendMessage(JSONArray args,
CallbackContext callbackContext)
throws JSONException {
String header = args.getString(0);
String body = args.getString(1);
String routerIp = args.getString(2);
int routerPort = args.getInt(3);
Tcp tcp = new Tcp();
String result = tcp.sendMessage(header,body,routerIp,routerPort);
callbackContext.success(result);
}
最后,我们到达sendMessage
中的方法Tcp
:
public String sendMessage(String header,String body, String routerIp, int routerPort) {
String respond = "";
Socket socket = null ;
try {
//Create socket
socket = new Socket(routerIp, routerPort);
socket.setSoTimeout(10000);
//Send header
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));
// You can use "UTF8" for compatibility with the Microsoft virtual machine.
wr.write(header);
wr.flush();
//Send data
wr.write(body);
wr.flush();
// Response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while((line = rd.readLine()) != null)
respond += line;
} catch (IOException e) {
e.printStackTrace();
} catch (Error e){
e.printStackTrace();
}finally{
final Pattern p = Pattern.compile("errorCode");
final Matcher m = p.matcher(respond);
if(!(m.find())){
return respond;
}else{
return "no Respond from " + routerIp + " on Port:" + routerPort;
}
}
}
首先:感谢您到目前为止的阅读: - )
我做了shure,Tcp类中的header-string保持不变。但是到达Tcp类的响应因提供者而异。使用Vodafone,Response看起来像:
预期结果如下:
我猜我的提供者(= vodafone)出于某些原因操纵我的GET请求。这个假设可能吗?有没有办法获得arround呢?
根据Koterpillar的要求:
请求:
部首:
GET /dd.htm?DD1 HTTP/1.1
Host: 134.3.157.233
Connection: keep-alive
Authorization: Basic ************
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Linux; Android 4.1.1; HTC One S Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Mobile Safari/537.36
Referer: http://134.3.157.223/mobile.htm
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3;
身体:(空)
正确回应
HTTP/1.1 200 OKAccess-Control-Allow-Origin: *Connection: closeContent-Type: text/htmlCache-Control: no-cache<html><head><title>ANEL-Elektronik NET-PwrCtrl</title><link href="np.css" rel="stylesheet" type="text/css"><script language='JavaScript' src='valid.js'></script><form method=post action=dd.htm name=fr><table width=570><tr><td class=tb_hd1>Einstellung Steckdose Nr. 1</table><table width=570 class=f><tr><td width=104>Name:<td><input maxlength=16 size=16 name="TN" value="Beamer"><td>Max. 16 Zeichen</table><table width=570 height=186 class=f><tr bgcolor=#EEEEEE height=22><td width=104>Timer<td width=25><td width=115>Wochentag<td width=58>von<td width=57>bis<td><tr><td bgcolor=#EEEEEE>Timer 1<td bgcolor="#b0e0e0"><input type=checkbox name="T00" ><td><input size=6 name="T10" value=""> 1=So.<td><input size=3 onblur="return VT(this.value, this.name);" name="T20" value="00:00"><td><input size=3 onblur="return VT(this.value, this.name);" name="T30" value="00:00"><td style="font-size:8pt;">bei 99:99 - Schaltzeit aus.<tr><td bgcolor=#EEEEEE>Timer 2<td bgcolor="#b0e0e0"><input type=checkbox name="T01" checked><td><input size=6 name="T11" value="12345"> 2=Mo.<td><input size=3 onblur="return VT(this.value, this.name);" name="T21" value="00:00"><td><input size=3 onblur="return VT(this.value, this.name);" name="T31" value="06:00"><td><tr><td bgcolor=#EEEEEE>Timer 3<td bgcolor="#b0e0e0"><input type=checkbox name="T02" ><td><input size=6 name="T12" value="346"> 3=Di.<td><input size=3 onblur="return VT(this.value, this.name);" name="T22" value="00:13"><td><input size=3 onblur="return VT(this.value, this.name);" name="T32" value="00:00"><td><tr><td bgcolor=#EEEEEE>Timer 4<td bgcolor="#b0e0e0"><input type=checkbox name="T03" checked><td><input size=6 name="T13" value=""> 4=Mi.<td><input size=3 onblur="return VT(this.value, this.name);" name="T23" value="28:49"><td><input size=3 onblur="return VT(this.value, this.name);" name="T33" value="03:00"><td><tr><td bgcolor=#EEEEEE>Zufallstimer<td bgcolor="#b0e0e0"><input type=checkbox name="T04" ><td><input size=6 name="T14" value=""> 5=Do.<td><input size=3 onblur="return VT(this.value, this.name);" name="T24" value="00:00"><td><input size=3 onblur="return VT(this.value, this.name);" name="T34" value="00:00"><td>Intervall <input size=3 onblur="return VT(this.value, this.name);" name="T4" value="00:00"><font color=#CCCCCC> um 00:00</font><tr><td bgcolor=#EEEEEE colspan=6></table><table width=570 class=f><tr><td bgcolor="#F0F0F0" colspan="2">Nach dem Neustart (Stromausfall) Steckdose:<tr><td bgcolor="#FAFAFA" colspan="2"><input type=radio name="TF" value="e" >einschalten <input type=radio name="TF" value="w" checked>Zustand wiederherstellen <input type=radio name="TF" value="a" >ausschalten<tr><td bgcolor="#E0E0E0" colspan="2"><tr><td bgcolor="#FAFAFA"><input type=checkbox name="TH" > Steuerung der Steckdose sperren.<td align=right bgcolor=#FAFAFA><input type=submit value=Speichern name="TS"></table></form><form method=post action=dd.htm id=a><div class="result ok" style="display:none">Ping - Test erfolgreich</div><div class="result fail" style="display:none">Ping - Test nicht erfolgreich</div><table style="font-size:7pt;" width=570><tr><td colspan=8 class=tb_hd>Keepalive Timer<tr bgcolor=#EEEEEE align=center><td colspan=2>Sende an die IP<td>ein Ping<td>alle (m)<td>und abschalten<td>wenn kein<br>Echo mal:<td>Reset<br>f�r (s)<td>nach (m)<br>fortsetzen<input type="hidden" name="KS"><tr align="center"><td bgcolor="#b0e0e0"><input type=checkbox name="K0" ><td><input size=15 name="K1" value="0.0.0.0"><td><input id=p type="submit" name="KA" value="Test"><td><input size=1 maxlength=3 style="text-align:center;" name="K2" value="1"><td><input type="submit" name="KB" value="Reset"><td><input size=1 maxlength=3 style="text-align:center;" name="K3" value="3"> x<td><input size=1 maxlength=3 style="text-align:center;" name="K4" value="60"><td><input size=1 maxlength=3 style="text-align:center;" name="K5" value="3"><tr bgcolor=#EEEEEE align=center><td colspan=2><td>
错误回复(是的,这些符号实际上就是我得到的)
HTTP/1.1 200 OKContent-Length: 1464 Content-Encoding: gzipAccess-Control-Allow-Origin: *Content-Type: text/htmlCache-Control: no-cacheConnection: keep-alive���������������]o�0��'�?�l�H�E4�T��LMmRo&'1��q���~NX �*��$������cG�Y匦$2�F�g���`+��W0�,����g�Dt�K��+�[b$7N�5�F�<�s��a�5�Uy�.҉��ʬ�ý/����h���<u~���w�ռe�ְf&/R�)��^H������^�*��X0�����ҵkR�I!Tkl�y� ��0!J��ܰd���L9��V/H�X�%w�����!q�)��V0�U���U��$Z��5��sv=G��ҭ6��$g�,7�^��TY�,)D��٤��D��t��L��A;�ȏ�r`hv�]![��$��]����ߕ�Bg���Hu{�j�*.��E@Z$kx{v�������<;�BƢ�3���}���\;��OPW��P#��]w��/��-Uo��T��"-i��x�1W�Č�h4���$��<2n���k��8{G9{{㖲��Q�ڔ]Qn��W{38�������[�ox7��'�t���bO���m�Z��A;���q8�Z�^�-�Tm�S��£���aX���i�Rg�7�F�7���������W�o�0�WN�4QM���b�UT�Ъj�Ӿ|��ĉ���������pF�@ �c?�|��^Z��*GM�����e>n�LJ"�xr�O�����@MG��`F|��%j A�!XoAbXdy�s�-�d�$�̓�ƌ�;!g|�!]��@�I�w�~���0�B�#9S\P�C_���x���?J�[��:7>��\5eWP�_���|w"�,P�ţe;6/KQkT|T��泚j�J�L���bS1�Q-S�ӈ��l�J�0�t|�e�4�n2|)iD\ϊ(�d'*� �,��?c�(�cH��� �&��5/drI�P� ����S��i ��X�ٱ8&��W���p�Ĕ���T�n�js�FV�֕�D%2D�d� "���ttz����`U�[^Q)�Q��J���6!{��3̹u��(cqs�s��p.�m(%�Jx˒�e���F ��~��}�a�Ա9����e�SZ�ϒ�^߬���s�y]�R����&�����E��X�����f�ϟ�)����pM�uԹSц��Nf9��)�_����{�ᐹ9��B����Y��v��uՙ�^��G��ү�����<:H��u��AO�~yM#9�o�=d]*R�)�ТR7#*R��Т��Ou���t+���������C�ӱ�lX>Se�L�B��]f�FJ~ri.0n��SK\sRAL�J��L%MH������B����B`Ir]�mIQi�uyf^J~�0�@�g~i��\w����J:��ֵ@W�Nl���`��
答案 0 :(得分:4)
AFAICT,您声称接受gzip编码的内容,但实际上并不想/支持它。
删除Accept-Encoding
标题,或将其更正为您支持的内容